public class SynBlock implements Runnable{
	private int shares = 20;
	private String s="a";
	public void run(){
		while(true){
			synchronized(s){
				if(shares > 0){//只允许票号大于0.
					try{
						Thread.sleep(300);//睡眠0.3秒.
					}catch(InterruptedException e){
						e.printStackTrace();
					}
					System.out.println(
							Thread.currentThread().getName()+
							" 销售票号 "+shares--);
				}else{
					break;
				}
			}
		}
	}

	public static void main(String[] args){
		SynBlock obj = new SynBlock();
		//下面3个线程共享 obj 对象.
		new Thread(obj,"A").start();//命名当前线程为A
		new Thread(obj,"B").start();
		new Thread(obj,"C").start();
	}
}
