java多处调用多任务_使用CompletableFuture进行多任务处理

package com.example.demo.research.async;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.concurrent.*;

import java.util.function.Supplier;

public class CompletableFutureDemo {

/**

* CPU核数

*/

private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(AVAILABLE_PROCESSORS,

3 * AVAILABLE_PROCESSORS,

3, TimeUnit.SECONDS,

new LinkedBlockingDeque<>(20));

public static void main(String[] args) throws Exception {

long startTime = System.currentTimeMillis();

System.out.println("demo start....." + startTime);

demo3();

System.out.println("demo end.....costTime = " + (System.currentTimeMillis() - startTime));

}

/**

* 基于allOf,并行处理多个任务,等待所有任务执行完毕后返回

*/

public static void demo3() throws Exception {

//用户整体接收各个任务的返回值

Map dataMap = new HashMap<>();

List> futureList = new ArrayList<>();

futureList.add(doSomethingA("A", dataMap));

futureList.add(doSomethingB("B", dataMap));

futureList.add(doSomethingC("C", dataMap));

CompletableFuture result = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));

try {

result.get(2, TimeUnit.SECONDS);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("result = " + dataMap);

//结果为:{doSomeThingB=B, doSomeThingA=A}

}

/**

* 基于thenCompose,第一个任务执行完后,第二个任务使用第一个任务的返回作为参数

*/

public static void demo1() throws Exception {

Map dataMap = new HashMap<>();

CompletableFuture completableFuture = doSomethingA("A", dataMap)

.thenCompose(id -> doSomethingB(id, dataMap));

String result = completableFuture.get(2, TimeUnit.SECONDS);

System.out.println("result = " + result);

//结果为:A is done is done

}

/**

* 基于thenCombine,当两个任务都完成后,使用两者的结果作为参数再执行一个异步任务

*/

public static void demo2() throws Exception {

Map dataMap = new HashMap<>();

CompletableFuture completableFuture = doSomethingA("A", dataMap)

.thenCombine(doSomethingB("B", dataMap), (a, b) -> a + " - " + b);

String result = completableFuture.get(2, TimeUnit.SECONDS);

System.out.println("result = " + result);

//结果为:A is done - B is done

}

/**

* @param dataMap 用户整体接收方法的返回值

*/

public static CompletableFuture doSomethingA(String taskId, Map dataMap) {

System.out.println("doSomethingA start....." + System.currentTimeMillis());

return CompletableFuture.supplyAsync(new Supplier() {

@Override

public String get() {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

dataMap.put("doSomeThingA", "A");

return taskId + " is done";

}

}, threadPoolExecutor);

}

public static CompletableFuture doSomethingB(String taskId, Map dataMap) {

System.out.println("doSomethingB start....." + System.currentTimeMillis());

return CompletableFuture.supplyAsync(() -> {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

dataMap.put("doSomeThingB", "B");

return taskId + " is done";

}, threadPoolExecutor);

}

public static CompletableFuture doSomethingC(String taskId, Map dataMap) {

System.out.println("doSomethingC start....." + System.currentTimeMillis());

return CompletableFuture.supplyAsync(() -> {

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

dataMap.put("doSomeThingC", "C");

return taskId + " is done";

}, threadPoolExecutor);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值