码迷,mamicode.com
首页 > 编程语言 > 详细

线程示例

时间:2017-11-18 16:39:17      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:ticket   rup   imp   rgs   示例   catch   sleep   thread   exception   

class Ticket implements Runnable{
	private int num = 100;
	Object obj = new Object();
	public void run(){
		while(true){
			synchronized(obj){
				if(num>0){
					try{
						Thread.sleep(10);
					}catch(InterruptedException e){}
					System.out.println(Thread.currentThread().getName()+"..sale.."+num--);
				}
			}
		}
	}
}
class SaleTicket{
	public static void main(String[] args){
		Ticket t = new Ticket();
		Thread s1 = new Thread(t);
		Thread s2 = new Thread(t);
		Thread s3 = new Thread(t);
		s1.start();
		s2.start();
		s3.start();
	}
}

  

线程示例

标签:ticket   rup   imp   rgs   示例   catch   sleep   thread   exception   

原文地址:http://www.cnblogs.com/guoyunhao/p/7857077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!