代码--模拟购买彩票系统

package csdn.lesson2;
public class TestTickets {
    public int tickets = 10;
    public synchronized void action(String name){
     System.out.println(name+"抢到了第"+tickets+"号票");
     tickets--;
    }
 public static void main(String[] args) {
  TestTickets tt = new TestTickets();
  TicketsThread t1 = new TicketsThread(tt,"小王");
  TicketsThread t2 = new TicketsThread(tt,"小李");
 }

}
class TicketsThread extends Thread{
 TestTickets tt;
 String name;
 public TicketsThread(TestTickets tt,String name){
  this.tt = tt;
  this.name = name;
  start();
 }
 public void run(){
  try{
   for(int i=0;i<5;i++){
    tt.action(name);
    Thread.sleep(50);
   }
  }catch(InterruptedException e){}
 }
}

posted @ 2012-05-22 21:41  yangkai_keven  阅读(219)  评论(0编辑  收藏  举报