package com.ygl;

/*

* 卖票属于多线程,卖票程序要被多个线程所执行,要写在run方法中

*

*/

public class Ticket extends Thread{

//定义ticketCount为static 则四个对象t1,t2,t3,t4可以实时共享ticketCount,但由于static

//修饰的类型成员生命周期太长,一般不使用

private static int ticketCount=100;

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

if(ticketCount>0){

System.out.println(Thread.currentThread().getName()+" sale:"+ticketCount--);

}

}

}

public static void main(String[] args) {

Ticket t1=new Ticket();

Ticket t2=new Ticket();

Ticket t3=new Ticket();

Ticket t4=new Ticket();


t1.start();

t2.start();

t3.start();

t4.start();

}

}