The use of join method

The join method is directly provided by Thread class. After Thread A calls Thread B's join method, it will be blocked and wait for B to finish.
If other threads call thread A's interrupt() method at this time, thread A will throw an InterruptException

 public static void main(String[] args) throws InterruptedException {
        Thread threadOne=new Thread(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("child threadOne over!");
        });



        Thread threadtwo=new Thread(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("child threadTwo over!");
        });


        System.out.println("time:"+System.currentTimeMillis());
        threadOne.start();
        threadtwo.start();
        System.out.println("wait all child thread over!");
        threadOne.join();
        threadtwo.join();
        System.out.println("time:"+System.currentTimeMillis());
        
    }

output

time:1563012587684
wait all child thread over!
child threadTwo over!
child threadOne over!
time:1563012588687

Here, the main thread calls the join method of threadOne, and it will block and wait for threadOne to finish executing before returning. The threadOne.join() method will only return after threadOne finishes executing.
After returning, call threadtwo.join() in the main thread and wait for threadTwo to finish.
Here, threadOne and ThreadTwo only execute for one second, so the final result is that the main thread is blocked for one second.

Modify the sleep time of ThreadOne and ThreadTwo to 5 seconds, 3 seconds, and the final waiting time is 5 seconds.

time:1563012942271
wait all child thread over!
child threadOne over!
child threadTwo over!
time:1563012947272

Change to

   System.out.println("time:"+System.currentTimeMillis());
        threadOne.start();
        threadtwo.start();
        System.out.println("wait threadOne child thread over!");
        threadOne.join();
        System.out.println("wait threadtwo child thread over!");
        threadtwo.join();
        System.out.println("time:"+System.currentTimeMillis());

output

time:1563013009495
wait threadOne child thread over!
child threadOne over!
wait threadtwo child thread over!
child threadTwo over!
time:1563013014500

If another thread calls the thread's interrupter method during the join, the InterruptedException will be thrown in the line where the thread calls the join() and returned immediately.

//Get main thread
        Thread mainThread=Thread.currentThread();

        new Thread(()->{
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mainThread.interrupt();
            System.out.println("Interrupted main!");
        }).start();

Output is as follows

time:1563013331167
wait threadOne child thread over!
Interrupted main!
Exception in thread "main" java.lang.InterruptedException
	at java.lang.Object.wait(Native Method)
	at java.lang.Thread.join(Thread.java:1252)
	at java.lang.Thread.join(Thread.java:1326)
	at JoinDemo.main(JoinDemo.java:52)
child threadOne over!
child threadTwo over!

Keywords: Java

Added by rahish on Mon, 28 Oct 2019 21:10:01 +0200