java多线程_票售问题
以下是本人调试的售票代码 为什么运行一次后cpu就百分之百了,而eclipse就死了,求优化。。。
package cn.xu.thread;
class Ticket implements Runnable{
private int tickets =100;
public void run() {
while(true){
if(tickets>0)
System.out.println(Thread.currentThread().getName()+" "+ tickets--);
}
}
}
public class ThreadDemo01 {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
} |
|