public class SynBlockMethod implements Runnable{
	private int millis=300;
	private int shares = 20;
	String s="a";

	public synchronized void func() {
		if(shares > 0){//只允许票号大于0.
			try{
				Thread.sleep(millis);//毫秒.
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			System.out.println(
					Thread.currentThread().getName()+
					" 销售票号 "+shares--);
		}
	}
	public void run(){
		while(true){
			if(s.equals("syncMethod")){
				func();
			}else{
				synchronized (this) {
					if(shares > 0){
						try{
							Thread.sleep(millis);
						}catch(InterruptedException e){
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().
								getName()+"销售票号"+shares--);
					}
				}
			}

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