public static void main(String[] args) throws InterruptedException { int peopleNum = 5000000;//吃饭人数 int tableNum = 5;//饭桌数量 ConcurrentLinkedQueue<String> info = new ConcurrentLinkedQueue<>(); CountDownLatch count = new CountDownLatch(tableNum);//计数器 //将吃饭人数放入队列(吃饭的人进行排队) for(int i=1;i<=peopleNum;i++){ info.offer("消费者_" + i); } //执行10个线程从队列取出元素(10个桌子开始供饭) System.out.println("-----------------------------------开饭了-----------------------------------"); long start = System.currentTimeMillis(); ExecutorService executorService = Executors.newFixedThreadPool(tableNum); for(int i=0;i<tableNum;i++) { executorService.submit(new Dinner("00" + (i+1), info, count)); System.out.println("EEEE桌号:"+(i+1) +"------info:"+info.size()); } //计数器等待,队列为空 count.await(); long time = System.currentTimeMillis() - start; System.out.println("-----------------------------------所有人已经吃完-----------------------------------"); System.out.println("共耗时:" + time); //停止线程池 executorService.shutdown(); } private static class Dinner implements Runnable{ private String name; private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>(); private CountDownLatch count; public Dinner(String name, ConcurrentLinkedQueue<String> queue, CountDownLatch count) { this.name = name; this.queue = queue; this.count = count; } @Override public void run() { //while (queue.size() > 0){ while (!queue.isEmpty()){ //从队列取出一个元素 排队的人少一个 //System.out.println("【" +queue.poll() + "】----已吃完..., 饭桌编号:" + name); String msg =queue.poll(); } count.countDown();//计数器-1 } }
ExecutorService线程池使用
最新推荐文章于 2021-09-03 09:42:35 发布