简体   繁体   English

Thread.join()和同步?

[英]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. 您是在问从线程T1调用T2上的join,T1中的代码在join()之后读取数据是否一定会看到T2编写的更改。 If that's the case, then the answer is yes, due to JLS 17.4.4 : 如果是这样,那么由于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. 线程T1中的最终操作与另一个线程T2中检测到T1已终止的任何操作同步。

T2 may accomplish this by calling T1.isAlive() or T1.join(). T2可以通过调用T1.isAlive()或T1.join()来实现。

and JLS 17.4.5 : JLS 17.4.5

All actions in a thread happen-before any other thread successfully returns from a join() on that thread. 线程中的所有操作都会发生-在任何其他线程成功从该线程上的join()返回之前

The method thread.join allows : 方法thread.join 允许

one thread to wait for the completion of another. 一个线程等待另一个线程的完成。 If t is a Thread object whose thread is currently executing, 如果t是当前正在执行线程的Thread对象,

 t.join(); 

causes the current thread to pause execution until t's thread terminates. 导致当前线程暂停执行,直到t的线程终止。

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. 如果只有两个线程,并且您等待读取器线程使用方法join等待写入器线程的结尾,则可以将其用作同步机制,但并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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