简体   繁体   中英

Thread.join() and synchronization?

在执行线程beeing时,Thread.join()是否与刷新缓存等完全同步?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM