这段java多线程代码有问题吗?



 public class Main {
public static void main(String[] args) {
MyThread mt = new MyThread();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}

class MyThread implements Runnable {
private int ticket = 20;

public void run() {
for (int i = 0; i < 20; i++) {
if (this.ticket > 0) {
System.out.println("线程:"+Thread.currentThread().getName()+" 卖票:ticket" + this.ticket--);
}
}
}
}

java 多线程 并发

帕♂秋♂莉♂ 11 years, 2 months ago

不知道你想要干什么。不过要是买票,可能下面才是对的。
public synchronized void run() ....

天道院美夏 answered 11 years, 2 months ago

Your Answer