java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program. If there are multiple threads calling the join() methods that means overloading on join allows the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. There are three overloaded join functions.
场景:多线程访问同一个变量叠加
package top.cloudmesh.join;
/**
* thread join 方法测试
*/
public class JoinDemo {
public static volatile int count = 0;
//多线程访问控制
public static synchronized void inccount(){
count++;
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0; i<10000; i++){
inccount();
}
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0; i<10000; i++){
inccount();
}
}
},"t2");
t1.start();
t2.start();
try {
t1.join();
System.out.println("main: "+Thread.currentThread().getState());
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("value: "+count);
}
}
What does this thread join code mean?
To quote from the Thread.join() method javadocs:
join() Waits for this thread to die.
There is a thread that is running your example code which is probably the main thread.
The main thread creates and starts the t1 and t2 threads. The two threads start running in parallel.
The main thread calls t1.join() to wait for the t1 thread to finish.
The t1 thread completes and the t1.join() method returns in the main thread. Note that t1 could already have finished before the join() call is made in which case the join() call will return immediately.
The main thread calls t2.join() to wait for the t2 thread to finish.
The t2 thread completes (or it might have completed before the t1 thread did) and the t2.join() method returns in the main thread.
It is important to understand that the t1 and t2 threads have been running in parallel but the main thread that started them needs to wait for them to finish before it can continue. That's a common pattern. Also, t1 and/or t2 could have finished before the main thread calls join() on them. If so then join() will not wait but will return immediately.
t1.join() means cause t2 to stop until t1 terminates?
No. The main thread that is calling t1.join() will stop running and wait for the t1 thread to finish. The t2 thread is running in parallel and is not affected by t1 or the t1.join() call at all.
In terms of the try/catch, the join() throws InterruptedException meaning that the main thread that is calling join() may itself be interrupted by another thread.
while (true) {
Having the joins in a while loop is a strange pattern. Typically you would do the first join and then the second join handling the InterruptedException appropriately in each case. No need to put them in a loop.
Main thread-->----->----->----->--block##########continue--->---->
\ | |
sub thread start()\ | join() |
\ | |
---sub thread----->--->--->--finish