码迷,mamicode.com
首页 > 其他好文 > 详细

synchronized 同步代码块,售票问题

时间:2018-12-02 20:38:08      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:void   static   同步   --   []   new   代码块   print   pack   

package cn.ljs.FristSync;


public class SalerDemo extends Thread {
	
	static int tickets = 1000;
	String name;
	static Object lock = new Object();
	public SalerDemo(String name) {
		this.name = name;
	}
	public void run(){
		
		while(true){
			
			synchronized (lock) {
				if (tickets < 1) {
					return ;
				}
				System.out.println(name + ": " + tickets);
				tickets--;
			}
		}
	}
	
	public static void main(String[] args) {
		Thread t1 = new SalerDemo("saler1");
		Thread t2 = new SalerDemo("saler2");
		Thread t3 = new SalerDemo("saler3");
		t1.start();
		t2.start();
		t3.start();
		
	}
	
	
}

  

synchronized 同步代码块,售票问题

标签:void   static   同步   --   []   new   代码块   print   pack   

原文地址:https://www.cnblogs.com/lijins/p/10054988.html

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