多线程卖票问题
我不要你觉得,我要我觉得,多线程就是牛皮,都听我的!!!!
package day22;
public class Ticker extends Thread {
static int tickers = 1;
static Object obj = "Tick";
public Ticker(String name) {
super(name);
}
@Override
public void run() {
super.run();
while (tickers <= 30) {
synchronized (obj) {
if (tickers <= 30) {
System.out.println(getName() + "卖出了第" + tickers + "张票");
tickers++;
} else {
System.out.println("售光了");
}
}
try {
sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Ticker t1 = new Ticker("林俊杰:");
Ticker t2 = new Ticker("邓紫棋:");
Ticker t3 = new Ticker("黄晓明:");
t1.start();
t2.start();
t3.start();
}
}