线程同步小例子:12306订票

package com.whut.study.syn;

public class Happy12306 {

	public static void main(String[] args) {
		Web12306 c = new Web12306(4,"happy sxt");
		//new Thread(c).start();
		new  Passenger(c,"老1",2)//Passenger 为代理对象
				.start();
		new  Passenger(c,"老2",1).start();
	}

}
//顾客
class Passenger extends  Thread{
	int seats;		
	public Passenger(Runnable target,String name,int seats) {
		super(target,name);
		this.seats = seats;
	}
	
}
//火车票网
class Web12306 implements Runnable{
	int available; //可用的位置
	String name; //名称
	public Web12306(int available, String name) {
		this.available = available;
		this.name = name;
	}
	
	public void run() {
		
			Passenger p = (Passenger)Thread.currentThread();
			boolean flag = this.bookTickets(p.seats);
			if(flag) {
				System.out.println("出票成功"+Thread.currentThread().getName()+"-<位置为:"+p.seats);
			}else {
				System.out.println("出票失败"+Thread.currentThread().getName()+"-<位置不够");
			}
	}
	//购票
	public synchronized boolean bookTickets(int seats) {
		System.out.println("可用位置为:"+available);
		if(seats>available) {
			return false;
		}
		available -=seats;
		return true;
	}
}

 

你可能感兴趣的:(线程同步小例子:12306订票)