package demo1;
//多个线程同时操作一个对象,线程不安全,数据紊乱
public class TestThread4 implements Runnable{
private int ticket = 10;
@Override
public void run() {
while (true){
if(ticket<=0){
break;
}
try {
Thread.sleep(100);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName()+"获得了第"+ticket--+"张票");
}
}
public static void main(String[] args) {
TestThread4 testThread4 = new TestThread4();
new Thread(testThread4,"小明").start();
new Thread(testThread4,"小蓝").start();
new Thread(testThread4,"小红").start();
new Thread(testThread4,"小三").start();
System.out.println(testThread4.ticket);
}
}
标签:int,问题,并发,初识,线程,ticket,public
From: https://www.cnblogs.com/qt0312/p/16787747.html