Java线程插队

当某个线程中调用其它线程的join()方法时,调用的线程将被阻塞,直到被join()方法加入的线程执行完成后才会继续运行。

示例:

public class ThreadJoin {
	public static void main(String[] args) throws InterruptedException {
		
		Thread thread = new Thread(new EmergencyThread(),"DeputyThread");
		thread.start();
		for(int i = 0 ;i < 50 ;i++) {
			System.out.println(Thread.currentThread().getName()+" is outputting :"+i);
			if(i == 10) {
				System.out.println("DeputyThread jump a queue");
				thread.join();
			}
			Thread.sleep(1000);
		}
		
	}
}

class EmergencyThread implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i = 0;i < 50;i++) {
			System.out.println(Thread.currentThread().getName()+" is outputting :"+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
}

猜你喜欢

转载自www.cnblogs.com/outxiao/p/10805656.html