/**
* Thread start() jdk.1.8.0 源码
*//**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/publicsynchronizedvoidstart() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/if (threadStatus != 0)
thrownew IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
// 调用了 native 的 start0 方法启动线程privatenativevoidstart0(); // native 本地方法
方法注释
start 是线程开始执行的原因;JVM 调用当前线程的 run 方法
结果是两个线程同时运行:
当前线程(调用 start方法获取响应 )
另一个线程(执行当前线程的run方法 )
/**
* Thread run() jdk.1.8.0 源码
*//**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/@Overridepublicvoidrun() {
if (target != null) {
target.run();
}
}
/* What will be run. */private Runnable target;
// 从源码上只是执行了当前 target 对象的run() ,而没有并发的相关实现
方法注释
如果该线程是使用独立的 Runnable 的 run 对象,那么 Runnable对象的run方法被调用;