求[Java] 纯文本查看 复制代码 public class Demo01 {
public static void main(String[] args) {
new Tickets().start();
new Tickets().start();
new Tickets().start();
new Tickets().start();
}
}
class Tickets extends Thread {
private static int tickets = 100;
public void run() {
synchronized(Tickets.class) {
while(true) {
if(tickets <= 0) {
break;
}else {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + "---第" + tickets-- + "张票");
}
}
}
}
}
把synchronized(Tickets.class)放在while(true)前面的话,会默认只调用第一个进程,这是为什么?
|