首页 > 编程语言 > 详细

Java线程插队

时间:2019-05-03 17:17:47      阅读:184      评论:0      收藏:0      [点我收藏+]

当某个线程中调用其它线程的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();
			}
			
		}
	}
	
}

 

Java线程插队

原文:https://www.cnblogs.com/outxiao/p/10805656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!