利用多執行緒寫一個賣票程式

mischen520發表於2017-08-16
package com.mischen.cn;

public class Demo2 {

	public static void main(String[] args) {
	SaleTicket s1=new SaleTicket();
	Thread t1=new Thread(s1,"視窗1");
	Thread t2=new Thread(s1,"視窗2");
	Thread t3=new Thread(s1,"視窗3");
	t1.start();
	t2.start();
	t3.start();
	}

}
class SaleTicket implements Runnable{
    
	int count=50;
	
	@Override
	public void run() {
		while(true){
			synchronized("鎖"){
				if(count>0){
					System.out.println(Thread.currentThread().getName()+"售出了第"+count+"張票");
					count--;
				}else{
					System.out.println("售罄了..");
					break;
				}
			}
		}
		
	}
	
}

相關文章