package com.Java;标签:testThread4,多个,Thread,对象,titckNum,start,线程,new From: https://www.cnblogs.com/fc666/p/17142190.html
//多个线程操作同一个对象
//抢火车票例子
//发现问题 多个线程抢夺同个资源的情况下 线程不安全 数据紊乱
public class TestThread4 implements Runnable{
private int titckNum = 10;
@Override
public void run() {
while(true){
try {
// 模拟延时
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (titckNum<=0){
break;
}
System.out.println(Thread.currentThread().getName()+"-->抢到了第"+titckNum--+"张票");
}
}
public static void main(String[] args) {
TestThread4 testThread4 = new TestThread4();
new Thread(testThread4,"小明").start();
new Thread(testThread4,"老师").start();
new Thread(testThread4,"黄牛").start();
}
}