import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 异步编排帮助类
**/
@Slf4j
@Service
public class CompletableFutureMain {
/**
* 构建线程池
*
* @return executorService
*/
private static ThreadPoolExecutor getExecutorServer() {
AtomicInteger threadCounter = new AtomicInteger();
return new ThreadPoolExecutor(
20,
20,
60L,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000),
r -> new Thread(r, "thread-" + threadCounter.getAndIncrement()),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
runAsync();
}
/**
* 异步无返回值
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void runAsync() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
}, getExecutorServer());
System.out.println("main...end..." + future.get());
}
/**
* 异步有返回值
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void supplyAsync() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer());
Integer integer = future.get();
System.out.println("main...end..." + integer);
}
/**
* 异步完成后感知结果与异常
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void whenComplete() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的感知
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer())
// 虽然能得到异常信息,但是没法修改返回数据
.whenComplete((res, exception) -> System.out.println("异步任务成功完成了...结果是: " + res + ", 异常是: " + exception))
// 可以感知异常,同时返回默认值
.exceptionally(throwable -> 10);
Integer integer = future.get();
System.out.println("main...end..." + integer);
}
/**
* 异步完成后处理结果与异常
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void handle() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer())
// 虽然能得到异常信息,但是没法修改返回数据
.handle((result, exception) -> {
if (result != null) {
return result * 2;
}
if (exception != null) {
return 0;
}
return 1;
});
Integer integer = future.get();
System.out.println("main...end..." + integer);
}
/**
* 串行化执行 不能获取到上一步的执行结果,无返回值
*/
public static void thenRunAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer())
// 线程串行化,thenRun: 不能获取到上一步的执行结果,无返回值
.thenRunAsync(() -> System.out.println("任务2启动了..."), getExecutorServer());
System.out.println("main...end...");
}
/**
* 串行化执行 能接收上一步的执行结果,无返回值
*/
public static void thenAcceptAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer())
// 线程串行化,thenAccept: 能接收上一步的执行结果,无返回值
.thenAcceptAsync((result) -> System.out.println("任务2启动了..." + result), getExecutorServer());
System.out.println("main...end...");
}
/**
* 串行化执行 能接收上一步的执行结果,有返回值
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void thenApplyAsync() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果: " + i);
return i;
}, getExecutorServer())
// 线程串行化,thenApply: 能接收上一步的执行结果,有返回值
.thenApplyAsync((result) -> {
System.out.println("任务2启动了..." + result);
return result + 2;
}, getExecutorServer());
System.out.println("main...end..." + future.get());
}
/**
* 两个任务都完成后执行 不能感知结果, 无返回值
*/
public static void runAfterBothAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
System.out.println("任务2结束: " + "hello");
return "hello";
}, getExecutorServer());
// 不能感知结果, 无返回值
future1.runAfterBothAsync(future2, () -> System.out.println("任务3启动..."), getExecutorServer());
System.out.println("main...end...");
}
/**
* 两个任务都完成后执行 能感知结果, 无返回值
*/
public static void thenAcceptBothAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
System.out.println("任务2结束: " + "hello");
return "hello";
}, getExecutorServer());
// 能感知结果,无返回值
future1.thenAcceptBothAsync(future2, (f1, f2) -> System.out.println("任务3开始...之前的结果" + f1 + ", " + f2), getExecutorServer());
System.out.println("main...end...");
}
/**
* 两个任务都完成后执行 能感知结果, 有返回值
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void thenCombineAsync() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
System.out.println("任务2结束: " + "hello");
return "hello";
}, getExecutorServer());
// 能感知结果,有 返回值
CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
System.out.println("任务3开始...之前的结果" + f1 + ", " + f2);
return "f3: " + f1 + ", " + f2;
}, getExecutorServer());
System.out.println("main...end..." + future.get());
}
/**
* 两个任务任意一个完成后执行 不能感知结果, 无返回值
*/
public static void runAfterEitherAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2结束: " + "hello");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, getExecutorServer());
// 两个任务,只要有一个完成,我们就执行任务3,不能感知结果, 无返回值
future1.runAfterEitherAsync(future2, () -> System.out.println("任务3启动..."), getExecutorServer());
System.out.println("main...end...");
}
/**
* 两个任务任意一个完成后执行 能感知结果, 无返回值
*/
public static void acceptEitherAsync() {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2结束: " + "hello");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, getExecutorServer());
// 两个任务,只要有一个完成,我们就执行任务3,能感知结果, 无返回值
future1.acceptEitherAsync(future2, (result) -> System.out.println("任务3启动..." + result), getExecutorServer());
System.out.println("main...end...");
}
/**
* 两个任务任意一个完成后执行 能感知结果, 有返回值
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void applyToEitherAsync() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1启动: " + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束: " + i);
return i;
}, getExecutorServer());
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2启动: " + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2结束: " + "hello");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, getExecutorServer());
// 两个任务,只要有一个完成,我们就执行任务3,能感知结果, 有返回值
CompletableFuture<String> future = future1.applyToEitherAsync(future2, (result) -> {
System.out.println("任务3启动..." + result);
return result + ", 哈哈";
}, getExecutorServer());
System.out.println("main...end..." + future.get());
}
/**
* futureGet阻塞式等待
*
* @throws ExecutionException 执行异常
* @throws InterruptedException 中断异常
*/
public static void futureGet() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息");
return "hello.jpg";
});
CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性");
return "蓝色 + 512G";
});
CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品介绍");
return "苹果";
});
// 可以,阻塞式等待,冗余代码,麻烦
String s = futureImg.get();
String s1 = futureAttr.get();
String s2 = futureDesc.get();
}
/**
* 所有任务都完成再执行
*/
public static void allOf() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息");
return "hello.jpg";
}, getExecutorServer());
CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性");
return "蓝色 + 512G";
}, getExecutorServer());
CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品介绍");
return "苹果";
}, getExecutorServer());
// 等待所有结果都完成
CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
allOf.get();
System.out.println("main...end..." + futureImg.get() + ", " + futureAttr.get() + ", " + futureDesc.get());
}
/**
* 任意一个任务完成再执行
*/
public static void anyOf() throws ExecutionException, InterruptedException {
System.out.println("main...start...");
// 方法完成后的处理
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息");
return "hello.jpg";
}, getExecutorServer());
CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性");
return "蓝色 + 512G";
}, getExecutorServer());
CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品介绍");
return "苹果";
}, getExecutorServer());
// 等待任意一个完成
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
anyOf.get();
System.out.println("main...end..." + anyOf.get());
}
}
ComplatableFuture异步编排
于 2023-03-01 18:19:25 首次发布