static { for (int i = 0; i < 10000; i++) tickets.add("票编号:" + i); }
publicstaticvoidmain(String[] args){ for (int i = 0; i < 10; i++) { new Thread(() -> { while (true) { synchronized (tickets) { if (tickets.size() <= 0) break;
/** * 有 N 张火车票,每张票都有一个编号,同时又10个窗口对外售票,请写一个模拟程序。 * * 分析下面的程序可能会产生那些问题? * 重复销售?超量销售? * * ConcurrentLinkedQueue 的 poll 方法,采用了 CAS 机制来实现加锁,所以不会有问题。 */ publicclassTicketSeller4{ static Queue<String> tickets = new ConcurrentLinkedQueue<>();
static { for (int i = 0; i < 10000; i++) tickets.add("票编号:" + i); }
publicstaticvoidmain(String[] args){ for (int i = 0; i < 10; i++) { new Thread(() -> { while (true) { String s = tickets.poll(); if (s == null) break; else System.out.println(Thread.currentThread().getName() + ":销售了--" + s); } }).start(); } } }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !