public class SynMethod implements Runnable{
	private int shares = 20;
	private String s="a";

	public synchronized void func() {
		if(shares > 0){//只允许票号大于0.
			try{
				Thread.sleep(300);//睡眠0.3秒.
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			System.out.println(
					Thread.currentThread().getName()+
					" 销售票号 "+shares--);
		}
	}
	public void run(){
		while(true){
			func();
			if(shares<=0){
				break;
			}
		}
	}

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