工作中用到了Thread,一開始用錯了,仔細研究了一下,稍作整理。
前言,今天寫代碼居然這樣寫的
new Thread() { @Overridepublic void run() {System.out.println("test");} }.run();
天真得以為這樣這樣會新開啟一個線程執行,可是列印了線程的資訊之後,發現還是在主線程中。仔細一想,確實我這樣做和調用一個新的Object的toString方法有區別麼,根本不會產生神馬新的線程。
當然我們的主要實現是在run裡面,這也是Thread唯一implements Runnable的一個方法。
於是我們調用start方法,可以在新線程中執行run中的處理邏輯,於是會一位start方法調用了run方法
然後卻又不是這樣
start的方法代碼
/** * Starts the new Thread of execution. The <code>run()</code> method of * the receiver will be called by the receiver Thread itself (and not the * Thread calling <code>start()</code>). * * @throws IllegalThreadStateException if the Thread has been started before * * @see Thread#run */ public synchronized void start() { if (hasBeenStarted) { throw new IllegalThreadStateException("Thread already started."); // TODO Externalize? } hasBeenStarted = true; VMThread.create(this, stackSize); }
請注意上面的javadoc The code run() method of the receiver will be called by the receiver Thread itself (and not the Thread calling start() )
而究竟是誰這麼隱蔽地調用run方法呢
其實是start間接地調用,start調用VMThread的create方法,而系統(OS建立需要的線程),然後JVM調用run方法。直接調用的是jvm。
下面是一篇很geek的文章講解:
start method
public void start() – this method is responsible for causing the thread (it’s called upon) to begin execution. The name may seem to be a misnomer here as this method only causes and not actually starts the execution. It just schedules the thread and when the
CPU Scheduler picks this thread for excution then the JVM calls the run() method to actually start the execution of the thread.
This method will obviously result into two concurrent threads – one, from which this method is called and two, the new thread which executes the run() method of the new Thread instance.
A thread will throw IllegalStateException in case you try to call the start() method on an already started thread instance. That means, a thread can not be started more than once. As per the Java specifications a thread may not be restarted after completing
its execution. You’ll be required to create and start the execution of a new thread in that case.
run method
public void run() – this is the only method of the Runnable interface and the classes which intend to execute their code in a separate thread of execution first implement the Runnable interface and subsequently define this method and put all the code expected
to be executed in the separate thread inside the scope of this run method.
The other way, such classes can follow for the same is by extending the Thread class and in that case also they should oevrride the run method of the Thread class and put all the code supposed to be executed in the new thread inside the scope of the overriden
run method.
Difference between start() and run() methods
start() methods only schedules the thread for execution and not actually begins the execution of the thread. The execution of the thread is started when the JVM calls the run() method of the thread once the CPU Scheduler picks this scheduled thread for execution.
文章來源:http://geekexplains.blogspot.com/2008/05/start-method-vs-run-method-in-java.html