package com.sixi.erp.coreservice.annotation; import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; public class Test { public static void main(String[] args) throws InterruptedException { Test test = new Test(); System.out.println(test.thread(1000, 20)); } private int thread(int taskCount, int threadCount) throws InterruptedException { //总操作次数计数器 AtomicInteger atomicInteger = new AtomicInteger(); //使用CountDownLatch来等待所有线程执行完成 CountDownLatch countDownLatch = new CountDownLatch(threadCount); //使用IntStream把数字直接转为Thread IntStream.rangeClosed(1, threadCount).mapToObj(i -> new Thread(() -> { //手动把taskCount分成taskCount份,每一份有一个线程执行 IntStream.rangeClosed(1, taskCount / threadCount).forEach(j -> increment(atomicInteger)); //每一个线程处理完成自己那部分数据之后,countDown一次 countDownLatch.countDown(); })).forEach(Thread::start); //等到所有线程执行完成 countDownLatch.await(); //查询计数器当前值 return atomicInteger.get(); } private int threadpool(int taskCount, int threadCount) throws InterruptedException { //总操作次数计数器 AtomicInteger atomicInteger = new AtomicInteger(); //初始化一个线程数量=threadCount的线程池 ExecutorService executorService = Executors.newFixedThreadPool(threadCount); //所有任务直接提交到线程池处理 IntStream.rangeClosed(1, taskCount).forEach(i -> executorService.execute(() -> increment(atomicInteger))); //提交关闭线程池申请,等待之前所有任务执行完成 executorService.shutdown(); executorService.awaitTermination(1, TimeUnit.HOURS); //查询计数器当前值 return atomicInteger.get(); } private int forkjoin(int taskCount, int threadCount) throws InterruptedException { //总操作次数计数器 AtomicInteger atomicInteger = new AtomicInteger(); //自定义一个并行度=threadCount的ForkJoinPool ForkJoinPool forkJoinPool = new ForkJoinPool(threadCount); //所有任务直接提交到线程池处理 forkJoinPool.execute(() -> IntStream.rangeClosed(1, taskCount).parallel().forEach(i -> increment(atomicInteger))); //提交关闭线程池申请,等待之前所有任务执行完成 forkJoinPool.shutdown(); forkJoinPool.awaitTermination(1, TimeUnit.HOURS); //查询计数器当前值 return atomicInteger.get(); } private int stream(int taskCount, int threadCount) { //设置公共ForkJoinPool的并行度 System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", String.valueOf(threadCount)); //总操作次数计数器 AtomicInteger atomicInteger = new AtomicInteger(); //由于我们设置了公共ForkJoinPool的并行度,直接使用parallel提交任务即可 IntStream.rangeClosed(1, taskCount).parallel().forEach(i -> increment(atomicInteger)); //查询计数器当前值 return atomicInteger.get(); } private int completableFuture(int taskCount, int threadCount) throws InterruptedException, ExecutionException { //总操作次数计数器 AtomicInteger atomicInteger = new AtomicInteger(); //自定义一个并行度=threadCount的ForkJoinPool ForkJoinPool forkJoinPool = new ForkJoinPool(threadCount); //使用CompletableFuture.runAsync通过指定线程池异步执行任务 CompletableFuture.runAsync(() -> IntStream.rangeClosed(1, taskCount).parallel().forEach(i -> increment(atomicInteger)), forkJoinPool).get(); //查询计数器当前值 return atomicInteger.get(); } private void increment(AtomicInteger atomicInteger) { atomicInteger.incrementAndGet(); try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }
多线程累加lambda几种方法(极客时间Java业务开发常见错误100例)
最新推荐文章于 2022-02-28 16:10:51 发布