多线程经典案例 卖票问题的一个疑问

public class test{
public static void main(String arg[]){
MyThread my = new MyThread();
new Thread(my,"seller A").start();
new Thread(my,"seller B").start();
new Thread(my, "seller C").start();
}

}
class MyThread implements Runnable{
private int ticket =5;
@Override
public void run() {
for (int x=0;x if(this.ticket>0) {
try {
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+
" sell,ticket " + this.ticket--);
}
}
}
}
既然JMM的机制是各个线程单独将主存中的数据拷贝进独立的cache中,那为什么还会出现 sell ticket -1 这种情况?

http://blog.csdn.net/linglongxin24/article/details/52807454?locationNum=1&fps=1

你这就属于是并发时产生了脏数据。所以在关键代码处需要同步处理。