JUC并发编程学习笔记 (7)

三种任务执行效果对比,代码示例 public class ForkJoinTest { public static void main(String[] args) throws ExecutionException, InterruptedException { test3(); } public static void test3() throws ExecutionException, InterruptedException { long start = System.currentTimeMillis(); MyForkJoinTask myForkJoinTask = new MyForkJoinTask(0L, 10_0000_0000L); ForkJoinPool forkJoinPool = new ForkJoinPool(); ForkJoinTask<Long> task = forkJoinPool.submit(myForkJoinTask); Long sum = task.get(); long end = System.currentTimeMillis(); System.out.println("输出结果:"+sum+"耗时:"+(end-start)); } //输出结果:500000000500000000耗时:2007 public static void test2(){ long start = System.currentTimeMillis(); long sum = 0; for (long i = 0; i <= 10_0000_0000L; i++) { sum+=i; } long end = System.currentTimeMillis(); System.out.println("输出结果:"+sum+"耗时:"+(end-start)); } //输出结果:500000000500000000耗时:341 public static void test1(){ long start = System.currentTimeMillis(); long sum = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0L, Long::sum); long end = System.currentTimeMillis(); System.out.println("输出结果:"+sum+"耗时:"+(end-start)); } //输出结果:500000000500000000耗时:192 } //没有返回值的话继承RecursiveAction public class MyForkJoinTask extends RecursiveTask<Long> { private Long start; private Long end; public MyForkJoinTask(Long start, Long end) { this.start = start; this.end = end; } // 临界值 private Long temp = 10000L; @Override protected Long compute() { if ((end - start) > temp) { //拆分任务 递归 Long middle = (start+end)/2; MyForkJoinTask forkJoinTaskLeft = new MyForkJoinTask(start, middle); forkJoinTaskLeft.fork(); MyForkJoinTask forkJoinTaskRight = new MyForkJoinTask(middle+1, end); forkJoinTaskRight.fork(); return forkJoinTaskLeft.join()+forkJoinTaskRight.join(); } else { Long sum = 0L; for (long i = start; i <= end; i++) { sum += i; } return sum; } } } CompletableFuture

步骤

异步执行

成功回调

失败回调

代码示例 public class CompletableFutureTest { public static void main1(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<String> completableFuture = new CompletableFuture<>(); //静态方法 CompletableFuture.runAsync(() -> { System.out.println(Thread.currentThread().getName() + "thenRunAsync"); return; }); System.out.println(Thread.currentThread().getName() + " end"); } /** * main end * ForkJoinPool.commonPool-worker-1thenRunAsync */ public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { System.out.println("supplyAsync ok"); //报错 System.out.println(10/0); return 200; }); CompletableFuture<Integer> completableFuture = future.whenComplete((u, e) -> { System.out.println(u);// 正常的返回结果 System.out.println(e);//错误信息 }).exceptionally(e -> { System.out.println(e.getMessage()); return 500; }); System.out.println(completableFuture.get()); } /** * 没报错: * supplyAsync ok * 200 * null * 200 * * 报错: * supplyAsync ok * null * java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero * java.lang.ArithmeticException: / by zero * 500 */ } JMM

什么是JMM

JMM是Java内存模型,是一种概念,约定,不是一种存在的东西。

JMM的同步概念

线程解锁前必须把共享变量立刻刷会主内存。

线程加锁前必须读取主内存中的最新值到工作内存中。

加锁和解锁必须是同一把锁。

JMM的8种原子操作

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxsxy.html