编辑代码

package pp;

public class TestSYN2 {
	public static void main(String[] agrs) throws Exception{
		myrunable2 my2=new myrunable2();
		Thread th1=new Thread(my2,"窗口1");
		Thread th2=new Thread(my2,"窗口2");
		th1.start();
		th2.start();
		
}
}
class myrunable2 implements Runnable{
    int ticket=50;
    Object obj=new Object();
	@Override
	public void run() {
		method();
	} 
	public synchronized void method(){
		while(true){
			if(ticket<=0) 
				break;
			else
			{	
                
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	System.out.println(Thread.currentThread().getName()+":在售卖第"+ticket--+"票");	
		}
		}
	}


}