来自文档:
The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,
t.join();
causes the current thread to pause execution until t's thread terminates
我无法理解的是,这是线程上的一种方法,与调用它的方法不同。因此,如果一个线程 t1
调用另一个线程的 t2.join()
,则 t2
对 t1
一无所知。那么,到底发生了什么让 t1
等待 t2
完成呢?
最佳答案
通过查看Java源代码:
从 t1 调用 t2.join() 将使 t1 等待 t2 对象(t2 是一个 Thread
,它是 Object
的子类)。只要 t1 还活着,等待就会永远持续下去。当 t2 线程完成其工作时,它将调用 Object.notifyAll()
,以便 t1 唤醒。
关于java - thread.join() 从概念上讲是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53405013/