CompletableFuture的详细用法

1. runAsync、supplyAsync

// 无返回值
public 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)

 

2. whenComplete、whenCompleteAsync

// 执行完成时,当前任务的线程执行继续执行 whenComplete 的任务。
public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action)
// 执行完成时,把 whenCompleteAsync 这个任务提交给线程池来进行执行。
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)

whenComplete用法如下:

CompletableFuture<String> future=CompletableFuture.supplyAsync(()->{
			System.out.println(GetCurrentTime()+" thread1="+Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "hello world1";
		});

		future.whenComplete((res,error)->{
			System.out.println(GetCurrentTime()+" thread22="+Thread.currentThread().getName());
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		future.whenComplete((res,error)->{
			System.out.println(GetCurrentTime()+" thread33="+Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		
		System.out.println(GetCurrentTime()+" thread3="+Thread.currentThread().getName());
		
		TimeUnit.SECONDS.sleep(10);

  输出结果:

2019-10-15 11:13:48 thread1=ForkJoinPool.commonPool-worker-1
2019-10-15 11:13:48 thread3=main
2019-10-15 11:13:49 thread33=ForkJoinPool.commonPool-worker-1
2019-10-15 11:13:50 thread22=ForkJoinPool.commonPool-worker-1

  多次执行都是一样结果,whenComplete中的任务是使用一个线程串行执行,并且后面的whenComplete先执行,可以通过调换两个whenComplete的顺序得到如下结果:

2019-10-15 11:15:56 thread3=main
2019-10-15 11:15:56 thread1=ForkJoinPool.commonPool-worker-1
2019-10-15 11:15:57 thread22=ForkJoinPool.commonPool-worker-1
2019-10-15 11:15:58 thread33=ForkJoinPool.commonPool-worker-1

whenCompleteAsync用法如下:

		CompletableFuture<String> future=CompletableFuture.supplyAsync(()->{
			System.out.println(GetCurrentTime()+" thread1="+Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "hello world1";
		});

		future.whenCompleteAsync((res,error)->{
			System.out.println(GetCurrentTime()+" thread22="+Thread.currentThread().getName());
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		future.whenCompleteAsync((res,error)->{
			System.out.println(GetCurrentTime()+" thread33="+Thread.currentThread().getName());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		
		System.out.println(GetCurrentTime()+" thread3="+Thread.currentThread().getName());
		
		TimeUnit.SECONDS.sleep(10);

 输出结果:

2019-10-15 11:09:30 thread1=ForkJoinPool.commonPool-worker-1
2019-10-15 11:09:30 thread3=main
2019-10-15 11:09:31 thread22=ForkJoinPool.commonPool-worker-2
2019-10-15 11:09:31 thread33=ForkJoinPool.commonPool-worker-1

  可以看到whenCompleteAsync从ForkJoinPool.commonPool随机获取新的线程执行,并且两个whenCompleteAsync的任务是并行执行

3. thenApply、handle

//当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
//与thenApply的区别是可能是新的线程 public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) //与thenApply效果差不多,出现异常不会走thenApply,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);

4. thenAccept、thenRun

//thenAccept 接收任务的处理结果,并消费处理。无返回结果。
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);
//thenRun 跟 thenAccept 方法不一样的是,不关心任务的处理结果。只要上面的任务执行完成,就开始执行 thenRun。
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);

5. thenCombine、thenAcceptBoth

public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);

public <U,V> CompletionStage<V> thenAcceptBoth(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenAcceptBothAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenAcceptBothAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);

  thenCombine、thenAcceptBoth 都是用来合并任务 —— 等待两个 CompletionStage 的任务都执行完成后,把两个任务的结果一并来处理。区别在于 thenCombine 有返回值;thenAcceptBoth 无返回值。

6. applyToEither、acceptEither、runAfterEither、runAfterBoth

  • applyToEither:两个 CompletionStage,谁执行返回的结果快,就用那个 CompletionStage 的结果进行下一步的处理,有返回值。
  • acceptEither:两个 CompletionStage,谁执行返回的结果快,就用那个 CompletionStage 的结果进行下一步的处理,无返回值。
  • runAfterEither:两个 CompletionStage,任何一个完成了,都会执行下一步的操作(Runnable),无返回值。
  • runAfterBoth:两个 CompletionStage,都完成了计算才会执行下一步的操作(Runnable),无返回值。

7. thenCompose

 thenCompose 方法允许你对两个 CompletionStage 进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作

posted @ 2019-10-15 12:42  myTang  阅读(4219)  评论(0)    收藏  举报