Java join 线程控制用法

JDK说明:

join

public final void join()
                throws InterruptedException
等待该线程终止。
抛出:
InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除

测试代码:

public class MyThread extends Thread {

	public static void main(String[] args) throws InterruptedException {
		A a=new A();
		B b=new B();
		a.start();
		a.join();
		b.start();
		
	}
}
class A extends Thread{
	
	public void run(){
		for(int i=0;i<10000;i++){
			System.out.print("A   "+i);
		}
	}
}

class B extends Thread{
	
	public void run(){
		for(int i=0;i<10000;i++){
			System.out.print("B   "+i);
		}
	}
}

 可以看出 等线程A 执行完之后 线程B才开始执行

非常清楚是不是呵呵

I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
原文地址:https://www.cnblogs.com/IamThat/p/2970647.html