public class MyThread implements Runnable{
private int ticket = 50;
public void run() {
for(int i = 0;i
if(ticket>0){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+
"线程,ticket = "+(ticket--));
}
}
}
}
public class SyncDemo {
public static void main(String[] args) {
// MyThread mt = new MyThread();
// Thread t1 = new Thread(mt,"线程A");
// Thread t2 = new Thread(mt,"线程B");
// Thread t3 = new Thread(mt,"线程C");
// t1.start();
// t2.start();
// t3.start();
new Thread(new MyThread(),"线程A").start();
new Thread(new MyThread(),"线程B").start();
new Thread(new MyThread(),"线程c").start();
}
}
为什么采用注释的方式票数最后为负数,而采用非注释的方式则不会出现票数为负数的情况