java线程立刻执行_Java 线程调用start()后会立即执行run()方法吗?
別想當(dāng)然
問(wèn)題
Java 線程調(diào)用start()后會(huì)立即執(zhí)行run()方法嗎?
我們?cè)陂_(kāi)發(fā)中,經(jīng)常和線程打交道,有些東西總是司空見(jiàn)慣,想當(dāng)然地認(rèn)為某些事情理所當(dāng)然...
但是今天偶然發(fā)現(xiàn)一個(gè)有趣的現(xiàn)象:
class Test {
public static void main(String[] args) {
System.out.println("hello https://tool.lu/");
Runnable runnable = new Runnable(){
@Override
public void run() {
System.out.println("--run()--");
}
};
Thread thread = new Thread(runnable);
thread.start();
System.out.println("--after start()--");
}
}
運(yùn)行結(jié)果如下:
運(yùn)行結(jié)果
看到運(yùn)行結(jié)果之后,我愣了一下,WTF?
跟我想的有點(diǎn)不一樣啊,
理論上來(lái)講,線程調(diào)用start()之后,將會(huì)調(diào)用該線程的run();
我又趕緊去查看了Java的官方文檔:JavaAPI
Thread的start()
Cause this thread to begin execution ; the Java Virtual Machine calls the run method of this thread.
通過(guò)上面我們可以發(fā)現(xiàn)start()就是使該線程開(kāi)始運(yùn)行,JVM會(huì)調(diào)用線程的run()方法。
那我們只能從源碼中找答案啦。
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the run method of this thread.
*
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* start method) and the other thread (which executes its
* run method).
*
* 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() {
/**
* 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".
*/
// Android-changed: throw if 'started' is true
if (threadStatus != 0 || started)
throw new 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);
started = false;
try {
nativeCreate(this, stackSize, daemon);
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 */
}
}
}
private native static void nativeCreate(Thread t, long stackSize, boolean daemon);
關(guān)鍵代碼已經(jīng)貼出,但是似乎關(guān)鍵在于nativeCreate()方法。該方法屬于c方法,有時(shí)間再追蹤一下。
思考
通過(guò)代碼測(cè)試我們發(fā)現(xiàn)線程調(diào)用start()方法,并不會(huì)立刻執(zhí)行run()方法。但是兩者之間的時(shí)間差是多少呢?
測(cè)試用例,有興趣的朋友can have a try.
把運(yùn)行結(jié)果給出來(lái):
運(yùn)行結(jié)果
通過(guò)我的多次測(cè)試,run()和start()的時(shí)間差一般都在[0,8]之內(nèi),當(dāng)然運(yùn)行足夠多次會(huì)發(fā)現(xiàn)時(shí)間差會(huì)更大。
總結(jié)
雖然該用例看似沒(méi)什么卵用,但不親自上手嘗試,還真會(huì)回答錯(cuò)...有趣有趣...
需要注意的是如果我們?cè)诰€程中創(chuàng)建對(duì)象,而在start()方法后直接使用該對(duì)象,就會(huì)出現(xiàn)該問(wèn)題測(cè)試用例。
夫小惑易方,大惑易性...
總結(jié)
以上是生活随笔為你收集整理的java线程立刻执行_Java 线程调用start()后会立即执行run()方法吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java代码题目_java题目代码?
- 下一篇: java判断斐波那契数列_Java 实例