栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多线程操作初步了解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多线程操作初步了解

文章目录
  • java多线程操作
  • (使用)CompletableFuture
  • 1、==runAsync和supplyAsync==
    • 示例
  • 2、在线程完成时的回调函数
    • 示例
  • 3、将线程串行化(thenApple)
    • 示例
  • 4、==thenCombine 合并任务==
    • 示例
  • 5、allOf 多种结合的调用
    • 示例

java多线程操作 (使用)CompletableFuture 1、runAsync和supplyAsync
public static CompletableFuture runAsync(Runnable runnable)
public static CompletableFuture runAsync(Runnable runnable, Executor executor)
public static  CompletableFuture supplyAsync(Supplier supplier)
public static  CompletableFuture supplyAsync(Supplier supplier, Executor executor)
  • runAsync没有返回值

  • supplyAsunc有返回值

  • Executor executor代表线程池,如果没有配置线程池将使用默认线程池(ForkJoinPool.commonPool())

示例
CompletableFuture>> f1 =
        CompletableFuture.supplyAsync(() -> this.mapper.accessNumByDay(Map), executor);
2、在线程完成时的回调函数
public CompletableFuture whenComplete(BiConsumer action)
public CompletableFuture whenCompleteAsync(BiConsumer action)
public CompletableFuture whenCompleteAsync(BiConsumer action, Executor executor)
public CompletableFuture exceptionally(Function fn)
  • 可以看到Action的类型是BiConsumer它可以处理正常的计算结果,或者异常情况。
    • super T为当前CompletableFuture的结果类型
  • whenComplete 和 whenCompleteAsync 的区别:
    • whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务。
      whenCompleteAsync:是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
示例
f1.whenComplete((list, throwable) -> System.out.println("任务一:" + System.currentTimeMillis()));
3、将线程串行化(thenApple)

当一个线程的输入依赖另一个线程的输出时,将线程串行化

public  CompletableFuture thenApply(Function fn)
public  CompletableFuture thenApplyAsync(Function fn)
public  CompletableFuture thenApplyAsync(Function fn, Executor executor)
  • super T 上一个线程的返回值
  • extends U 当先线程的返回值
示例
private static void thenApply() throws Exception {
    CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {
        @Override
        public Long get() {
            long result = new Random().nextInt(100);
            System.out.println("result1="+result);
            return result;
        }
    }).thenApply(new Function() {
        @Override
        public Long apply(Long t) {
            long result = t*5;
            System.out.println("result2="+result);
            return result;
        }
    });
    
    long result = future.get();
    System.out.println(result);
}
4、thenCombine 合并任务

将两个任务的结果合并

public  CompletionStage thenCombine(CompletionStage other,BiFunction fn);
public  CompletionStage thenCombineAsync(CompletionStage other,BiFunction fn);
public  CompletionStage thenCombineAsync(CompletionStage other,BiFunction fn,Executor executor);
  • 一个任务.thenCombine(另一个任务,(两个任务类型))
示例
CompletableFuture>> f1 =
        CompletableFuture.supplyAsync(() -> this.mapper.accessNumByDay(Map), executor);

CompletableFuture>> f2 =
        CompletableFuture.supplyAsync(() -> this.mapper.accessGroup(Map), executor);

CompletableFuture> f3 = f1.thenCombine(f2, (list, list2) -> {
        Map resultMap = new HashMap<>();
        resultMap.put("accessByDay",list);
        resultMap.put("accessGroup",list2);
        return resultMap;
        }
);

return f3.join();
5、allOf 多种结合的调用 示例
@GetMapping("/getUserInfo5")
public String getUserInfo5() throws ExecutionException, InterruptedException {
    CompletableFuture future1
            = CompletableFuture.supplyAsync(() -> "biandan");
    CompletableFuture future2
            = CompletableFuture.supplyAsync(() -> "说");
    CompletableFuture future3
            = CompletableFuture.supplyAsync(() -> "让天下没有难写的代码");
 
    //返回的是 void 类型
    CompletableFuture combinedFuture_1
            = CompletableFuture.allOf(future1, future2, future3);
 
    //使用 join 方法手动获取结果
    String combinedFuture_2 = Stream.of(future1, future2, future3)
            .map(CompletableFuture::join)
            .collect(Collectors.joining(" "));
 
    return combinedFuture_2;
}
  • 注意 CompletableFuture.allOf 的返回类型是CompletableFuture。这种方法的局限性在于它不能返回所有 Supplier 的组合结果。我们必须从未来手动获取结果。使用 CompletableFuture.join() 方法获取。
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号