java join () method

When A thread execution thread to join B () method, it will wait for A, B and other execution threads are finished, A will be performed.

It can be used to join temporarily join thread.

class Demo implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+"......"+i);
        }
    }
}
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException {
        Demo d = new Demo();
        Thread t1 = new Thread(d);
        Thread t2 = new Thread(d);
        t1.start();
        t1.join();
        t2.start();
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName()+"......"+i);
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/hongxiao2020/p/12612163.html
Recommended