Java多线程异步处理

1.异步执行无返回值
 CompletableFuture noArgsFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("***************");
            }
        });
2.异步执行有返回值
 CompletableFuture<String> returnString = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                return "abc";
            }
        });
        String result = returnString.get();
        System.out.println(result);
3.多个异步执行,等待所有结果返回才算完成
 CompletableFuture<String> feature1 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                try {
                    Thread.sleep(3000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "abc";
            }
        });
        CompletableFuture<String> feature2 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                return "def";
            }
        });
        CompletableFuture<String> feature3 = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                return "ghi";
            }
        });
        CompletableFuture totalFeature = CompletableFuture.allOf(feature1, feature2, feature3);
        totalFeature.join();
        String str3=feature3.get();
        String str2=feature2.get();
        String str1=feature1.get();
        List<String> stringList = Stream.of(feature1, feature2, feature3).map(CompletableFuture::join).collect(Collectors.toList());
        System.out.println(stringList);
        System.out.println(str1+str2+str3);
4.多个异步执行,有参数有返回值
        //模拟入参
        List<Date> dates = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            dates.add(sdf.parse("2020-02-22 12:12:01"));
            dates.add(sdf.parse("2020-02-23 12:12:01"));
            dates.add(sdf.parse("2020-02-24 12:12:01"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<CompletableFuture<String>> futures = new ArrayList<>();
        //将任务提交,并异步执行
        for (Date date : dates) {
            CompletableFuture<String> newFeature = CompletableFuture.supplyAsync(new Supplier<String>() {
                @Override
                public String get() {
                    //异步处理逻辑,这里将日期转换为字符串
                    return coverLongToString(date);
                }
            });
            futures.add(newFeature);
        }
        //所有任务执行完成返回才会继续,不然会阻塞在这里等待
        //TODO 这里怎么能够设置下超时时间
        CompletableFuture.allOf(futures.iterator().next());
        //拿到所有异步线程返回的结果
        List<String> dateStringList = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
        System.out.println(dateStringList);

你可能感兴趣的:(Java8,java)