//多个线程操作同一个对象
//买火车票的例子
//问题:多个线程操作同一个对象,发生数据紊乱,线程不安全
public class test4Thread implements Runnable{
private int ticknumber=10;
public void run() {
//模拟延时
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true){
if(ticknumber<=0){
break;
}
System.out.println
(Thread.currentThread().getName()+"拿到了第"+ticknumber--+"张票");
}
}
public static void main(String[] args) {
test4Thread t1=new test4Thread();
new Thread(t1,"a").start();
new Thread(t1,"b").start();
new Thread(t1,"c").start();
}
}