/** * Alipay.com Inc. * Copyright (c) 2004-2018 All Rights Reserved. */ package com.weigu.controller; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @author 886644 * @version $Id: ConcurrentLinkedQuene.java, v 0.1 2018/9/4 16:23 886644 Exp $ */ public class ConcurrentLinkedQueneTest { /** * 通过无锁的方式,实现了高并发下的高性能, */ private static ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>(); //~~线程个数 private static int count = 2; //~~CountDownLatch,一个同步铺住类,允许一个或者多个线程一直等待; private static CountDownLatch countDownLatch = new CountDownLatch(count); public static void main(String[] args) throws InterruptedException { long timeMillis = System.currentTimeMillis(); ExecutorService es = Executors.newFixedThreadPool(4); ConcurrentLinkedQueneTest.offer(); for (int i = 0; i < count; i++) { es.submit(new Poll()); } countDownLatch.await();//使的主线程阻塞,,直到为零才继续执行; es.shutdown(); } /** * 生产 */ private static void offer() { for (int i = 0; i < 10000; i++) { queue.offer(i); } } /** * add()和offer()添加的两个方法 * poll()和peek()都是取头元素节点,区别在于前者会删除元素,后者不会 */ static class Poll implements Runnable { @Override public void run() { while (!queue.isEmpty()) { System.out.println(queue.poll()); } countDownLatch.countDown(); } } }
ConcurrentLinkedQuene --------并发队列
最新推荐文章于 2023-12-15 22:05:29 发布