Join方法 09

join:

     1. join合并线程,待此线程执行完成后,在执行其它线程,其它线程阻塞

     2. 可以想象成插队

package Runnable1;
//测试jion方法
public class TestJoin implements Runnable
{
   @Override
   public void run()
  {
       for (int j = 0; j < 10; j++)
      {
           System.out.println("线程vip来了"+j);
      }
  }
   public static void main(String[] args) throws InterruptedException
  {
       TestJoin testJoin=new TestJoin();
       Thread thread=new Thread(testJoin);


       //主线程
       for (int i = 0; i < 20; i++)
      {
        if (i==10)
        {
            thread.start();
            thread.join();//插队
        }
           System.out.println("main"+i);
      }
  }
}
/*
main0
main1
main2
main3
main4
main5
main6
main7
main8
main9
线程vip来了0
线程vip来了1
线程vip来了2
线程vip来了3
线程vip来了4
线程vip来了5
线程vip来了6
线程vip来了7
线程vip来了8
线程vip来了9
main10
main11
main12
main13
main14
main15
main16
main17
main18
main19

*/
-------------------------​
package src;
class SimpleThread extends Thread
{
   SimpleThread(String s)
  {
       super(s) ;
  }
   public void run()
  {
       for(int i=0;i<3;i++)
      {
           System.out.println(getName()+":"+i) ;
      }
  }
}
public class ThreadJion1
{
   public static void main(String args[])
  {
       SimpleThread t1=new SimpleThread("first") ;
SimpleThread t2=new SimpleThread("second") ;  
t1.start() ;      
       try
      {
           t1.join() ;
      }
       catch(InterruptedException e) {}
       t2.start() ;      
       try
      {
           t2.join() ;
      }
       catch(InterruptedException e) {}
       System.out.println("主线程运行!");        
  }
}

posted @ 2022-07-27 17:16  zjw_rp  阅读(14)  评论(0)    收藏  举报