package execisetest;
class TicketThread extends Thread{
public static int total=20;
public void run(){
while(this.total>0)
{
System.out.println(TicketThread.currentThread()+"还剩"+total+"张票");
this.total=this.total-1;
}
}
}
public class SaleTicket {
public static void main(String [] args){
TicketThread t1=new TicketThread();
TicketThread t2=new TicketThread();
TicketThread t3=new TicketThread();
t1.start();
t2.start();
t3.start();
}
}
结果
Thread[Thread-0,5,main]还剩20张票
Thread[Thread-0,5,main]还剩19张票
Thread[Thread-0,5,main]还剩18张票
Thread[Thread-0,5,main]还剩17张票
Thread[Thread-0,5,main]还剩16张票
Thread[Thread-0,5,main]还剩15张票
Thread[Thread-0,5,main]还剩14张票
Thread[Thread-0,5,main]还剩13张票
Thread[Thread-0,5,main]还剩12张票
Thread[Thread-0,5,main]还剩11张票
Thread[Thread-0,5,main]还剩10张票
Thread[Thread-1,5,main]还剩20张票
Thread[Thread-1,5,main]还剩8张票
Thread[Thread-1,5,main]还剩7张票
Thread[Thread-1,5,main]还剩6张票
Thread[Thread-1,5,main]还剩5张票
Thread[Thread-1,5,main]还剩4张票
Thread[Thread-1,5,main]还剩3张票
Thread[Thread-1,5,main]还剩2张票
Thread[Thread-1,5,main]还剩1张票
Thread[Thread-2,5,main]还剩12张票
Thread[Thread-0,5,main]还剩8张票
为什么20 12 8出现了两次?每次运行结果可能不一样,但每次都有重复的 |
|