Java多线程编程核心技术(第3版)
上QQ阅读APP看书,第一时间看更新

1.2.11 方法run()被JVM所调用

使用如下代码启动一个线程:


public static void main(String[] args) {
    Thread t = new Thread();
    t.start();
}

那么Thread.java类的run()方法由JVM调用,这一点在start()方法的帮助文档中也有说明,如下:


/**
 * 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()
 */
public synchronized void start() {

当start()方法执行后,由JVM调用run()方法。