JAVA多线程售票问题

//定义一个类实现Runnable接口,定义一个需要同步的售票方法,然后重写run方法调用售票的sale方法
class SaleTicket implements Runnable{
	private int tickets = 100;
	
	
	private synchronized void sale(){
		if(tickets > 0){
			System.out.println(Thread.currentThread().getName() + "卖出 第 "+ (tickets--)+"张票");
			
			try{
				Thread.sleep(100);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}
	public void run(){
		while(tickets > 0){
			sale();
		}
	}
}


public class JavaTest {
		
	public static void main(String[] args){


		SaleTicket st = new SaleTicket();
		Thread t1 = new Thread(st, "一号窗口");
		Thread t2 = new Thread(st, "二号窗口");
		Thread t3 = new Thread(st, "三号窗口");
		Thread t4 = new Thread(st,"四号窗口 ");
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
	
		
	}
}


你可能感兴趣的:(java,thread,多线程,String,Class)