Java中 join线程的作用是什么

Java中 join线程的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

使调用join()的线程执行完毕后才能执行其它线程,在一定意义上,它可以实现同步的功能。

class TestThreadMethod extends Thread{  public static int shareVar = 0;  public TestThreadMethod(String name){  super(name);  }  public void run(){  for(int i=0; i<4; i++){  System.out.println(" " + i);  try{  Thread.sleep(3000);  }  catch(InterruptedException e){  System.out.println("Interrupted");  }  }  }  }  public class TestThread{  public static void main(String[] args){  TestThreadMethod t1 = new TestThreadMethod("t1");  t1.start();  try{  t1.join();  }  catch(InterruptedException e){}  t1.start();  }  }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对亿速云的支持。