Thread enforcement-Join

Thread enforcement-Join

  • Join merge threads, after this thread is executed, execute other threads, and other threads are blocked
  • It can be imagined as jumping in line.

Case

package Thread;

//测试Join方法
//想象为插队
public class TestJoin implements Runnable{
    
    

    @Override
    public void run() {
    
    
        for (int i = 0; i < 100; i++) {
    
    
            System.out.println("我是VIP我先执行"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
    
    

     //启动我们的线程
         TestJoin tj=new TestJoin();
         Thread thread=new Thread(tj);
         thread.start();

        for (int i = 0; i < 500; i++) {
    
    
            if (i == 100){
    
    
                thread.join();  //执行run方法 Vip 插队
            }
            System.out.println("main"+i);
        }

    }



}

Guess you like

Origin blog.csdn.net/qq_45162683/article/details/111591908