并行流适用场景-CPU密集型

匿名 (未验证) 提交于 2019-12-02 23:34:01
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/larva_s/article/details/90403578

1. 场景描述

2. 原因猜测

3. 测试

@Service public class ConsumMsg {      @Autowired     StreamOpt streamOpt;      /**      * 初始化任务队列      * @param count      * @return      */     public List<Integer> initMsg(int count) {         List<Integer> list = Lists.newArrayList();         for (int i = 0; i < count; i++) {             list.add(i);         }         return list;     }      /**      * 并行流处理      * @param count      */     public void parallel(int count) {         List<Integer> list = initMsg(count);         int successSize = list.parallelStream().mapToInt(streamOpt::sendMsg).sum();         System.out.println("并行流成功个数:" + successSize);     }      /**      * 线程池处理      * @param count       * @param maxThreadCount 最大线程数      * @param initThreadCount 初始线程数      * @param latch      * @throws ExecutionException      * @throws InterruptedException      */     public void normalThread(int count, int maxThreadCount, int initThreadCount, CountDownLatch latch) throws ExecutionException, InterruptedException {         List<Integer> list = initMsg(count);         // 等待的其他线程则存储到队列里面         ThreadPoolExecutor executor = new ThreadPoolExecutor(initThreadCount, maxThreadCount, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(60000)); //        int sum = 0;         for (int i = 0; i < list.size(); i++) {             Future result = executor.submit(new SendMsgThread(i, latch));             // 结果转换耗时很长,需要取消 //            sum += (Integer) result.get();         } //        System.out.println("线程池成功个数:" + sum);     }      /**      * 线程      */     class SendMsgThread implements Callable {          int msg;         CountDownLatch latch;          public SendMsgThread(int msg, CountDownLatch latch) {             this.msg = msg;             this.latch = latch;         }          @Override         public Integer call() throws Exception {             int result = streamOpt.sendMsg(msg);             latch.countDown();             return result;         }     } }  @Service public class StreamOpt {       public int sendMsg(int msg) {        // todo 网络调用         return 1;     }  } 

3.1 默认并行流线程数

public class ConsumTest  extends BaseTest {      @Autowired     ConsumMsg consumMsg;      @Test     public void test() throws InterruptedException, ExecutionException {         int count = 100;         CountDownLatch latch = new CountDownLatch(count);         Stopwatch stopwatch = Stopwatch.createStarted();         consumMsg.parallel(count);         System.out.println("并行流耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms");          System.out.println("==============");          // 线程池反而多了几种状态,比如countDownLatch计数,实验结果比并行流还快         Stopwatch watch = Stopwatch.createStarted();         consumMsg.normalThread(count, 120, 100, latch);         latch.await();         System.out.println("线程池耗时:" + watch.elapsed(TimeUnit.MILLISECONDS) + "ms");     } } 

3.2 提高并行流线程数

public class ConsumTest  extends BaseTest {      @Autowired     ConsumMsg consumMsg;      @Test     public void test() throws InterruptedException, ExecutionException {         int count = 100;         CountDownLatch latch = new CountDownLatch(count);          // 指定并行流线程数         System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "100");         Stopwatch stopwatch = Stopwatch.createStarted();         consumMsg.parallel(count);         System.out.println("并行流耗时:" + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms");          System.out.println("==============");          // 线程池反而多了几种状态,比如countDownLatch计数,实验结果比并行流还快         Stopwatch watch = Stopwatch.createStarted();         consumMsg.normalThread(count, 120, 100, latch);         latch.await();         System.out.println("线程池耗时:" + watch.elapsed(TimeUnit.MILLISECONDS) + "ms");     } } 

3.3 测试结果:6w个任务

4. 总结

4.1 I/O密集型

4.1 CPU密集型

5. 参考资料

http://www.importnew.com/14742.html

https://www.itranslater.com/qa/details/2110153885791814656

《java 8 in action》第七章 并行数据处理与性能

文章来源: https://blog.csdn.net/larva_s/article/details/90403578
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!