package ticketDemo;
class ticket implements Runnable{
private int count=100;
public void run(){
while(count>0){
System.out.println(Thread.currentThread().getName()+"卖出票"+count--);
}
}
}
public class ticketDemo {
public static void main(String[] args){
ticket t=new ticket();
Thread thread1=new Thread(t,"线程1");
Thread thread2=new Thread(t,"线程2");
thread1.start();
thread2.start();
}
}
输出的一次结果:线程1卖出票100
线程1卖出票99
线程1卖出票98
线程1卖出票97
线程1卖出票96
线程2卖出票100
线程2卖出票94
线程2卖出票93
线程2卖出票92
线程2卖出票91
线程2卖出票90
线程2卖出票89
线程2卖出票88
线程2卖出票87
线程2卖出票86
线程2卖出票85
线程2卖出票84
线程2卖出票83
线程2卖出票82
线程2卖出票81
线程2卖出票80
线程2卖出票79
线程2卖出票78
线程2卖出票77
线程2卖出票76
线程2卖出票75
线程2卖出票74
线程2卖出票73
线程2卖出票72
线程2卖出票71
线程2卖出票70
|
|