public class TestTicket {
public static void main(String[] args) {
Runnable st = new SellTicket(new Tick());
new Thread(st, "A").start();
new Thread(st, "B").start();
new Thread(st, "C").start();
new Thread(st, "D").start();
}
public static class SellTicket implements Runnable {
public Tick tick;
Object mutex = new Object();
public SellTicket(Tick tick) {
this.tick = tick;
}
public void run() {
while (tick.getCount() > 0) {
synchronized(mutex) { //需要有一个锁变量
if(tick.getCount() <=0) break; //synchronized之前没锁住其他线程(有可能进入到while等待,当进入后需要重新判断count值是大于0,不然就会变成0或负数)
int temp = tick.getCount();
System.out.println(Thread.currentThread().getName()
+ "-----sale" + temp--);
tick.setCount(temp);
}
}
}
}
public static class Tick {
private int count = 10;
private Tick() {
}
private static final class lazyhodler {
public static final Tick INSTANCE = new Tick();
}
public static final Tick getInstance() {
return lazyhodler.INSTANCE;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
结果:
A-----sale10
A-----sale9
A-----sale8
A-----sale7
A-----sale6
A-----sale5
A-----sale4
A-----sale3
A-----sale2
A-----sale1
java实现多线程的两种方式及售票实例https://www.cnblogs.com/jiansen/p/7347426.html