有两种方法可以确定线程是否已完成。首先,您可以调用isAlive()
在线程上。该方法由 Thread
定义。 ,其一般形式如下所示:
final boolean isAlive()
isAlive()
如果调用该方法的线程仍在运行,则该方法返回 true。否则返回 false。而isAlive()
偶尔有用,您更常用来等待线程完成的方法称为 join()
isAlive()
与 join()
。优点?
最佳答案
While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a thread to finish is called join( )
我不明白。在您看来,为什么 join()
比 isAlive()
更常用?你有证据吗?
它们是为 Thread 类定义的两个不同的方法:
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
join() :
Waits for this thread to die.
一种方法执行检查,另一种方法等待线程死亡。
当你想在不等待的情况下检查线程的生命周期时,你将使用 isAlive() ,它将返回一个关于线程状态的 boolean 值。
如果你想等待线程死亡,你将使用join()
。意思是要等待分配给线程的任务完成。
关于java - isAlive 并加入 JAVA 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500127/