Thread.join() and synchronization?

天大地大妈咪最大 提交于 2020-06-28 14:39:11

问题


Is Thread.join() a full synchronization with flushing of caches etc. when performed of the thread beeing joined?


回答1:


I think you're asking whether from thread T1 that calls join on T2, code in T1 reading data after the join() will definitely see changes written by T2. If that's the case, then the answer is yes, due to JLS 17.4.4:

The final action in a thread T1 synchronizes-with any action in another thread T2 that detects that T1 has terminated.

T2 may accomplish this by calling T1.isAlive() or T1.join().

and JLS 17.4.5:

All actions in a thread happen-before any other thread successfully returns from a join() on that thread.




回答2:


The method thread.join 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.

This is not related to synchronization, but only to sequence of steps.

If you have only two threads and you wait the reader thread waits the end of writer thread with method join, this can be used as a mechanism of synchronization, but it is not.



来源:https://stackoverflow.com/questions/37839010/thread-join-and-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!