该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
joinpublic final void join()
throws InterruptedException
等待执行了join方法的线程终止(执行完毕)。
public class TestJoin { public static void main(String[] args) { ThreadY tY = new ThreadY("ThreadYield" ); tY.start(); try { tY.join();//意思是把start()分开的两个线程合并了,等到tY执行完毕才继续下面的代码,所以tY的sleep无效。 } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i <= 10; i++) { System. out.println("I an main thread!" ); } }}
class ThreadY extends Thread { ThreadY(String s) { super(s); }
public void run() { for (int i = 0; i <= 10; i++) { System. out.println("I an " + this.getName()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); System. exit(-1); } } }}