哦,对啊,今天也讲过这个问题,那你看下面改正后的这一套代码:
public class Ticket implements Runnable {
private int tickets = 100;
@Override
public void run() {
while (true) {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第"
+ (tickets--) + "张票");
}
}
}
}
public class TicketDemo {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t, "窗口1");
Thread t2 = new Thread(t, "窗口2");
Thread t3 = new Thread(t, "窗口3");
Thread t4 = new Thread(t, "窗口4");
// 每一个线程对象都去掉用了run()方法
t1.start();
t2.start();
t3.start();
t4.start();
}
}
>>>我现在的水平还很有限,所以暂时只能做到这些,见谅! |