[java-project-gl]异步&线程池
异步&线程池
一、线程
1、初始化线程的4种方式
(1)、继承Thread
(2)、实现Runnable接口
(3)、实现Callable接口 + FutureTask (可以拿到返回结果,可以处理异常)
(4)、线程池
方式1和方式2:主进程无法过去线程的运算结果。不适合当前常见
方式3:主进程可以获取线程的运算结果,但是不利于控制服务器中的线程资源。可以导致服务器资源耗尽。
方式4:通过如下两种方式初始化线程池
Executors.newFiexedThreadPool(3);
//或者
new ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime,TimeUnit unit,workQueue,threadFactory,handler);
通过线程池性能稳定,也可以获取执行结果,并捕获异常。但是,在业务复杂情况下,一个异步调用可能会依赖于另一个异步调用的执行结果。
实例:
方式一:
public class ThreadTest {
public static void main(String[] args) {
System.out.println("main....start....");
Thread01 thread = new Thread01();
thread.start(); //启动线程
System.out.println("main....end....");
}
public static class Thread01 extends Thread{
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}
}
}
运行结果:
main....start....
main....end....
当前线程:12
运行结果:5
Process finished with exit code 0
方式二:
public class ThreadTest {
public static void main(String[] args) {
System.out.println("main....start....");
Runable01 runable01 = new Runable01();
new Thread(runable01).start();
System.out.println("main....end....");
}
public static class Runable01 implements Runnable {
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}
}
}
运行结果:
main....start....
main....end....
当前线程:12
运行结果:5
方式三:
public class ThreadTest {
public static void main(String[] args) {
System.out.println("main....start....");
FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
new Thread(futureTask).start();
//阻塞等待 等到整个线程执行完成,获取返回结果
try {
Integer integer = futureTask.get();
System.out.println("main....end...." + integer);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public static class Callable01 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}
}
}
运行结果:
main....start....
当前线程:12
运行结果:5
main....end....5
方式4
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
System.out.println("main....start....");
service.execute(new Runable01());
System.out.println("main....end....");
}
public static class Runable01 implements Runnable {
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}
运行结果:
main....start....
main....end....
当前线程:12
运行结果:5
总结
区别:
-
1、2不能得到返回值。3可以获取返回值
-
1、2、3都不能控制资源
-
4可以控制资源,性能稳定。
我们以后在业务代码里面,以上三种启动线程的方式都不用。【将所有的多线程异步任务都交给线程池执行】
2、线程池[ExecutorService]
创建:
1)、Executors;
2)、new ThreadPoolExecutor();
1、七大参数:
corePoolSize:[5]核心线程数[一致存在除非(allowCoreThreadTimeOut)];线程池,创建好以后就准备就绪的线程数量,就等待接收异步任务去执行
5个 Thread thread = new Thread(); thread.start();
maximumPoolSize:[200] 最大线程数量; 控制资源
keepAliveTime:存活时间。如果当前的线程数量大于core数量。
释放空闲的线程(maximumPoolSize-corePoolSize)。只要线程空闲大于指定的存活时间keepAliveTime;
unit:时间单位
BlockingQueue<Runnable> workQueue: 阻塞队列。如果任务有很多,就会将目前多的任务放在队列里面。
只要有线程空闲,就回去队列里面取出新的任务继续执行。
threadFactory:线程的创建工厂。
RejectedexecutionHandler handler:如果队列满了,按照我们指定的拒绝策略执行任务
2、运行流程:
1、线程池创建,准备好core数量的核心线程,准备接受任务。
2、新的任务进来,用core准备好的空闲线程执行。
(1)、core满了,就将再进来的任务放入阻塞队列中。空闲的core就会自己去阻塞队列获取任务执行
(2)、阻塞队列满了,就直接开新线程执行,最大只能开到max指定的数量
(3)、max都执行好了。Max-core 数量空闲的线程会在keepAliveTime指定的时间后自动销毁。最终保持到core大小
(4)、如果线程数开到了max的数量,还有新任务进来,就会使用reject 指定的拒绝策略进行处理
3、所有的线程创建都是由指定的factory创建的。
3、原生方式创建线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, //核心线程
200, //最大线程数量
10, //空闲存活时间
TimeUnit.SECONDS, //时间单位
new LinkedBlockingDeque<>(100000), //阻塞队列
Executors.defaultThreadFactory(), //线程的创建工厂
new ThreadPoolExecutor.AbortPolicy()); //拒绝策略
4、Executors常见的4种线程池
-
newCachedThreadPool
创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 -
newFixedThreadPool
创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 -
newScheduledThreadPool
创建一个定长线程池,支持定时及周期性任务执行。
-
newSingleThreadExecutor
创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务
5、面试题
问题:
一个线程池 core 7; max 20, que:50,100并发进来怎么分配的;
答案:
7个会立即得到执行,50个会进入队列,再开13个进行执行。剩下的30个就使用拒绝策略;
如果不想抛弃还要执行。CallerRunsPolicy;
6、开发中为什么使用线程池
- 降低资源的消耗
通过重复利用已经创建好的线程降低线程的创建和销毁带来的损耗 - 提高响应速度
因为线程池中的线程数没有超过线程池的最大上限时,有的线程处于等待分配任务的状态,当任务来时无需创建新的线程就能执行 - 提高线程的可管理性
线程池会根据当前系统特点对池内的线程进行优化处理,减少创建和销毁线程带来的系统开销。无限的创建和销毁线程不仅消耗系统资源,还降低系统的稳定性,使用线程池进行统一分配
二、completableFuture 异步编排
业务场景:
查询商品详情页的逻辑比较复杂,有些数据还需要远程调用,必然需要花费更多的时间。
假如商品详情页的每个查询,需要如下标注的时间才能完成。
那么,用户需要5.5s后才能看到商品详情页的内容。很显然是不能接受的。
如果有多个线程同时完成这6步操作,也许只需要1.5s即可完成响应。
1、创建异步对象
CompletableFuture 提供了四个静态方法来创建一个异步操作
static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<void> runAsync(Runnable runnable,Executor executor)
public static <U> CompletableFuture<U> supplyasync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,Executor executor)
1、runXxxx都是没有返回结果的,supplyXxxx都是可以获取返回结果的
2、可以传入自定义的线程池,否则就用默认的线程池;
实例:
runAsync:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
System.out.println("main....start....");
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}, service);
System.out.println("main....end....");
}
输出:
main....start....
main....end....
当前线程:12
运行结果:5
supplyasync:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, service);
Integer integer = future.get();
System.out.println("main....end...." + integer);
}
输出:
main....start....
main....end....
当前线程:12
运行结果:5
2、计算完成时回调方法
public CompletableFuture<T> whencomplete(BiConsumer<? super T,? super Throwable> action); public CompletableFuture<T> whencompleteAsync(BiConsumer<? super T,? super Throwable>
action);
public CompletableFuture<T> whencompleteAsync(BiConsumer<? super T,? super Throwable>
action,Executor executor);
public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn);
whenComplete可以处理正常和异常的计算结果,exceptionally处理异常情况。
whenComplete和 whenCompleteAsync的区别:
- whenComplete: 是执行当前任务的线程执行继续执行 whenComplete的任务。
- whenCompleteAsync:是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
方法不以Async 结尾,意味着Action使用相同的线程执行,而Async.可能会使用其他线程执行(如果是使用相同的线程池,也可能会被同一个线程选中执行)
实例:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 方法完成后的感知
*/
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 0; //自己造的异常
System.out.println("运行结果:" + i);
return i;
}, service).whenComplete((res,excption)->{
//whenComplete虽然能得到异常信息,但是没法修改数据
System.out.println("异步任务成功完成了...结果是:" + res + ";异常是:" + excption);
}).exceptionally(throwable -> {
//exceptionally 可以感知异常,同时返回默认值
return 10; //自定义异常返回结果
});
Integer integer = future.get();
System.out.println("main....end...."+ integer);
}
输出:
当前线程:12
异步任务成功完成了...结果是:null;异常是:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
main....end....10
3、handle 方法
public <U> CompletionStage<U> handle(BiFunction<? super T,Throwable,? extends U> fn);
public <U> CompletionStage<U> handleAsync(BiFunction<? super T,Throwable,? extends U>
fn);
public <U> Completionstage<U> handleAsync(BiFunction<? super T,Throwable,? extends U>
fn,Executor executor);
和complete一样,可对结果做最后的处理(可处理异常),可改变返回值。
实例:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 方法执行完成后的处理
*/
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 0; //自己造的异常
System.out.println("运行结果:" + i);
return i;
}, service).handle((res,thr)->{
if(res!=null){ //如果结果不为空
return res*2;
}
if (thr!=null){ //如果异常不为空
return 0;
}
return 0;
});
//R apply(T t,U u)
Integer integer = future.get();
System.out.println("main....end...."+ integer);
}
输出结果:
当前线程:12
main....end....0
4、线程串行化方法
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public Completionstage<Void> thenAcceptAsync(Consumer<? super T> action);
public Completionstage<void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
public Completionstage<void> thenRun(Runnable action);
public completionStage<void> thenRunAsync(Runnable action);
public completionstage<void> thenRunAsync(Runnable action,Executor executor);
thenApply 方法:当一个线程依赖另一个线程时,获取上一个任务返回的结果,并返回当前任务的返回值。
thenAccept方法:消费处理结果。接收任务的处理结果,并消费处理,无返回结果。
thenRun方法:只要上面的任务执行完成,就开始执行thenRun,只是处理完任务后,执行thenRun的后续操作
带有Async默认是异步执行的。同之前。加了Async后续任务就需要开新的线程执行,不加续用之前的线程
以上都要前置任务成功完成。
Function<?super T,?extends U>
T:上一个任务返回结果的类型
实例:
- thenRun
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 线程串行化
* 1)、thenRun:不能获取到上一步的执行结果
*/
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, service).thenRunAsync(() -> {
System.out.println("任务2启动了");
}, service);
System.out.println("main....end....");
}
输出:
当前线程:12
运行结果:5
main....end....
任务2启动了
- thenAccept
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 线程串行化
* 2)、thenAcceptAsync: 能接受上一步结果,但是无返回值
*/
CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, service).thenAcceptAsync(res->{
System.out.println("任务二启动了..."+res);
},service);
System.out.println("main....end....");
}
输出:
当前线程:12
运行结果:5
main....end....
任务二启动了...5
- thenApply
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 线程串行化
* 3)、thenApplyAsync:能接受上一步结果,有返回值
*/
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, service).thenApplyAsync(res -> {
System.out.println("任务2启动了..." + res);
return "Hello" + res;
}, service);
//future.get() get()为一个阻塞方法,直到上面拿到值才会返回结果
System.out.println("main....end...."+ future.get());
}
输出:
当前线程:12
运行结果:5
任务2启动了...5
main....end....Hello5
5、两任务组合 - 都要完成
public <U,V> CompletableFuture<V> thenCombine(
Completionstage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletableFuture<V> thenCombineAsync(
CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletableFuture<V> thenCombineAsync(
CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn,Executor executor);
public <U> CompletableFuture<void> thenAcceptBoth(
Completionstage<? extends U> other, BiConsumer<? super T,? super U> action);
public <U> CompletableFuture<void> thenAcceptBothAsync(
CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action);
public <U> CompletableFuture<void> thenAcceptBothAsync(
CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action,Executor executor);
public CompletableFuture<void> runAfterBoth(CompletionStage<?> other, Runnable action);
public CompletableFuture<void> runAfterBothAsync(CompletionStage<?> other, Runnab1e action);
public CompletableFuture<void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);
两个任务必须都完成,触发该任务。
thenCombine:组合两个future,获取两个future的返回结果,并返回当前任务的返回值
thenAcceptBoth:组合两个future,获取两个future任务的返回结果,然后处理任务,没有返回值。
runAfterBoth:组合两个future,不需要获取future的结果,只需两个future处理完任务后,处理该任务。
实例:
- runAfterBoth:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 两个都完成,不能接受结果,无返回值
*/
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:" );
return i;
}, service);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束:" );
return "Hello";
}, service);
future01.runAfterBothAsync(future02,()->{
System.out.println("任务3开始...");
},service);
}
输出:
任务1线程:12
任务1结束:
任务2线程:13
任务2结束:
任务3开始...
- thenAcceptBoth:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 两个都完成,可以接受上面的返回值,无自己返回值
*/
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:" );
return i;
}, service);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束:" );
return "Hello";
}, service);
future01.thenAcceptBothAsync(future02,(f1,f2)->{
System.out.println("任务3开始...之前的结果: " +f1+"-->>" + f2);
},service);
}
输出:
任务1线程:12
任务1结束:
任务2线程:13
任务2结束:
任务3开始...之前的结果:5->> Hello
- thenCombine
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
/**
* 两个都完成,可以接受上面的返回值,有自己的返回值
*/
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:" );
return i;
}, service);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2结束:" );
return "Hello";
}, service);
CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
return f1 + ":" + f2 + "-> Haha";
}, service);
System.out.println("main....end...." + future.get());
}
输出:
任务1线程:12
任务1结束:
任务2线程:13
任务2结束:
main....end....5:Hello-> Haha
6、两任务组合 - 一个完成
public <U> CompletableFuture<U> applyToEither(
CompletionStage<? extends T> other,Function<? super T,U> fn);
public <U> CompletableFuture<U> applyToEitherAsync(
CompletionStage<? extends T> other,Function<? super T,U> fn):
public <U> CompletableFuture<U> applyToEitherAsync(
CompletionStage<? extends T> other,Function<? super T,U> fn, Executor executor);
public CompletableFuture<void> acceptEither(
CompletionStage<? extends T> other,Consumer<? super T> action);
public CompletableFuture<void> acceptEitherAsync(
CompletionStage<? extends T> other,Consumer<? super T> action);
public completableFuture<void> acceptEitherAsync(
CompletionStage<? extends T> other,Consumer<? super T> action, Executor executor);
public CompletableFuture<void> runAfterEither(CompletionStage<?> other, Runnable action);
public CompletableFuture<void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);
public CompletableFuture<void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor);
当两个任务中,任意一个future任务完成的时侯,执行任务。
applyToEither:两个任务有一个执行完成,获取它的返回值,处理任务并有新的返回值。
acceptEither:两个任务有一个执行完成,获取它的返回值,处理任务,没有新的返回值。
runAfterEither:两个任务有一个执行完成,不需要获取future的结果,处理任务,也没有返回值。
实例:
- runAfterEitherAsync
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:");
return i;
}, service);
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
System.out.println("任务2结束:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello";
}, service);
/**
* 两个任务,只要有一个完成,就执行任务3
* runAfterEitherAsync:不感知结果,自己也无返回值
*
*/
future01.runAfterEitherAsync(future02, () -> {
System.out.println("任务3开始...之前的结果:");
}, service);
System.out.println("main....end....");
}
输出:
任务1线程:12
任务2线程:13
任务1结束:
main....end....
任务3开始...之前的结果:
任务2结束:
- acceptEitherAsync
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:");
return i;
}, service);
CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
System.out.println("任务2结束:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello";
}, service);
// acceptEitherAsync:感知结果,自己没有返回值
future01.acceptEitherAsync(future02, (res) -> {
System.out.println("任务3开始...之前的结果:" + res);
}, service);
System.out.println("main....end....");
}
输出:
任务1线程:12
任务2线程:13
任务1结束:
main....end....
任务3开始...之前的结果:5
任务2结束:
- applyToEitherAsync
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1结束:");
return i;
}, service);
CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
System.out.println("任务2结束:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello";
}, service);
//applyToEitherAsync:能接受上一步结果,有返回值
CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
System.out.println("任务3开始...之前的结果:" + res);
return res.toString() + "->哈哈";
}, service);
System.out.println("main....end...." + future.get());
}
输出:
任务1线程:12
任务1结束:
任务2线程:13
任务3开始...之前的结果:5
main....end....5->哈哈
任务2结束:
7、多任务组合
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs);
public static CompletableFuture<Object> anyOf (CompletableFuture<?>... cfs);
allOf:等待所有任务完成
anyOf;只要有一个任务完成
实例:
allOf:
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息");
return "hello.jpg";
},service);
CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性");
return "黑色+256G";
},service);
CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("查询商品介绍");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "华为";
},service);
CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
allOf.get(); //等待所有结果完成
System.out.println("main....end...."+futureImg.get()+"=>"+futureAttr.get()+"=>"+futureDesc.get());
}
输出:
查询商品的图片信息
查询商品的属性
查询商品介绍
main....end....hello.jpg=>黑色+256G=>华为
anyOf
public class ThreadTest {
//当前系统中池只有一两个,每个异步任务,提交给线程池让他自己去执行就行
public static ExecutorService service = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息");
return "hello.jpg";
},service);
CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性");
return "黑色+256G";
},service);
CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("查询商品介绍");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "华为";
},service);
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
anyOf.get(); //等待所有结果完成
System.out.println("main....end...." + anyOf.get());
}
输出:
查询商品的图片信息
查询商品的属性
main....end....hello.jpg
查询商品介绍
8、项目实战
线程执行顺序:
- 1创建异步操作先执行; 3,4,5等待1执行完,获取到1的结果再并行执行;
- 2不依赖1的关系,可以直接创建异步操作运行
- 等待全部线程运行完毕后,输出结果
/**
* 展示当前sku的详情
* @param skuId
* @return
*/
@Override
public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
SkuItemVo skuItemVo = new SkuItemVo();
//线程执行顺序: 1创建异步操作先执行; 3,4,5等待1执行完,获取到1的结果再并行执行;
// 2不依赖1的关系,可以直接创建异步操作运行
//创建异步操作,线程方法: supplyAsync,可以获取返回结果
CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
//1、sku基本信息获取 pms_sku_info
SkuInfoEntity info = getById(skuId);
skuItemVo.setInfo(info);
return info;
}, executor);
//3,4,5串行化线程方法 thenAcceptAsync 能接受上一步结果,但是无返回值
CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
//3、获取spu的销售属性组合
List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
skuItemVo.setSaleAttr(saleAttrVos);
}, executor);
CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
//4、获取spu的介绍 pms_spu_info_desc
SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
skuItemVo.setDesp(spuInfoDescEntity);
}, executor);
CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(res -> {
//5、获取spu的规格参数信息
List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
skuItemVo.setGroupAttrs(attrGroupVos);
}, executor);
//创建异步操作,线程方法: runAsync: 没有返回结果
CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
//2、sku的图片信息 pms_sku_images
List<SkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
skuItemVo.setImages(images);
}, executor);
//等待所有任务都完成
CompletableFuture.allOf(saleAttrFuture,descFuture,baseAttrFuture,imageFuture).get();
return skuItemVo;
}