[java]代码库package concurrent001;
public class BuyTicket extends Thread {
private int tickets;
private String sellName = "";
public BuyTicket(int num){
this.tickets = num;
}
@Override
public void run() {
while (tickets> 0) {
buyTicket();
}
}
public synchronized void buyTicket(){
if(tickets>0){
System.out.println(Thread.currentThread().getName() + "售出了" + tickets + "号票子");
tickets--;
}
}
public static void main(String[] args) throws InterruptedException {
BuyTicket bt = new BuyTicket(2000);
Thread t1 = new Thread(bt);
Thread t2 = new Thread(bt);
Thread t3 = new Thread(bt);
t1.start();
t2.start();
t3.start();
t1.join();t2.join();t3.join();
System.out.println("end................");
}
}//源代码片段来自云代码http://yuncode.net