package try51.thread.safe; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadSafeDemo { public static void main(String[] args) { //所有票源 ArrayList<Thread> lst = new ArrayList<>(); lst.add(new Thread(new Tickets("网上 售票"))); lst.add(new Thread(new Tickets("手机APP 售票"))); lst.add(new Thread(new Tickets("实地窗口售票"))); //随机产生一个客户端类型 Random rdom = new Random(); //设置一个线程池 ExecutorService es = Executors.newFixedThreadPool(100); //模拟有 15个客户端 来购票 for (int i = 0; i < 15; i++) { int index = rdom.nextInt(3); Thread thread = lst.get(index); //购票线程进入线程池 es.submit(thread); } //关闭线程池 es.shutdown(); } } /** * * @author lztkdr * */ class Tickets implements Runnable { //安全锁对象 public static Object locker = new Object(); //静态的票总数(固定) public static int TicketCount = 10; public String name; /** * * @param name 客户端 票源 */ public Tickets(String name) { this.name = name; } @Override public void run() { //线程锁 队列式 售票 synchronized (locker) { try { //模拟购票需要200毫秒 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } if (TicketCount > 0) { System.out.println(this.name + "\t出售1一张,剩余\t " + (--TicketCount)); } else { System.out.println(this.name + "\t沒有抢到票!!!"); } } } }