首页 > 开发 > JAVA > 正文

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

2017-09-07 09:23:48  来源:网友分享
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--);}}}}

解决方案

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