public class test {
public static void main(String[] args) {
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
MyThread t4=new MyThread();
t1.setName("A");
t2.setName("B");
t3.setName("C");
t4.setName("D");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
public class MyThread extends Thread{
public static int count=0;
@Override
public void run() {
while (true){
synchronized (MyThread.class){
if(count==100){
break;
}else {
count++;
System.out.println(getName()+"正在售卖第"+count+"张票。");
}
}
}
}
}
......
配置高的电脑可能100张票全是A售卖了,属于正常现象,可以把count的值改大一点。