use*_*465 7 java multithreading
这纯粹是一个理论问题,因为我不确定引起这个问题的条件会很常见.
比方说,你有一个线程,你用它的启动方法启动:
Thread c = new Thread();
c.start();
Run Code Online (Sandbox Code Playgroud)
然后直接在线程上调用Join()方法来告诉你所处的方法,直到线程被执行继续.
c.join();
Run Code Online (Sandbox Code Playgroud)
是不是可能在调用join方法之前执行并完成线程,因此让方法不知道在继续之前它必须等待c完成?我想你可以在调用start()方法之前尝试调用join()方法,但是每当我在测试用例中尝试这个时,就会出现错误.
任何人都知道可能的解决方案,或者JVM是否处理它?正如我所说,我无法触发这种情况,但从理论上说它有可能......
Ale*_*lov 10
根据Thread#join(long millis)源代码,该isAlive()方法用于检查线程状态.
/**
* Tests if this thread is alive. A thread is alive if it has
* been started and has not yet died.
*
* @return <code>true</code> if this thread is alive;
* <code>false</code> otherwise.
*/
public final native boolean isAlive();
Run Code Online (Sandbox Code Playgroud)
false如果线程已经完成,这个方法显然会返回,所以thread.join()会立即退出.
c.join()意味着当前正在执行的线程应该c在完成之前等待完成.如果c在调用之前完全执行c.join(),则满足该条件,如c已完成执行.你打电话的方法c.join()不会"知道"; c.join()就像一个无操作.
| 归档时间: |
|
| 查看次数: |
3787 次 |
| 最近记录: |