CompletableFuture

一、定义
CompletableFuture是java8中添加的一个类,这个类主要的作用就是提供了新的方式来完成异步处理,包括合成和组合事件的非阻塞方式。
管道式类,同lambda 流stream管道式处理一样,需要终端方法终止。

二、  CompletableFuture中的函数接口
函数接口是java8新引入的一个概念,使用函数接口的方法支持lambda表达式,在API中依@FunctionInterface标注。
Runnable:带run的方法参数,不带返回结果
Supplier:带supply的方法参数,带有返回结果。
Function:带apply的方法参数,带返回结果。
Consumer:带accept的方法参数,不带返回结果。
BiFunction: 处理三件事的方法参数,带返回结果。

参考:http://blog.csdn.net/a971236184/article/details/78583894


三、 类的目的
目的:多任务异步处理。
做一件事:
runAsync            不返回结果
supplyAsync      返回结果
exceptionally     捕捉异常
thenApply          做一件事,在其结果上处理,生成新的结果 
做两件事:
thenAccept             第一件事完成后做第二件事情(接受第一件事结果),不返回结果
thenRun                 第一件事完成后做第二件事(两件事无关系)
whenComplete     第一件事完成后做第二件事(接受第一件事结果,并捕捉第一件事产生的异常)
thenCompose       第一件事完成后做第二件事情(接受第一件事结果),返回结果

做三件事:  

thenAcceptBoth   两件事情完成后做第三件事情(接受两件事情的结果),返回结果
runAfterBoth     两件事情完成后做第三件事请(与前两件事无关系),不返回结果
thenCombine             两件事情完成后做第三件事请,返回结果

示例:

package org.zihao;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 测试CompletableFutures,主要用来处理异步任务。
 * 
 */
public class CompletableFutureTest {
	private static String first_thing = "first thing...";
	private static String first_thing_return = "first thing return...";
	private static String second_thing = "second thing...";
	private static String second_thing_return = "second thing return...";
	private static String third_thing = "third thing...";
	private static String third_thing_return = "third thing return...";


	public static void main(String[] args) {
		try {
			// thenApply();
			// thenCompose();
			// thenAcceptBoth();
		} catch (InterruptedException | ExecutionException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 做一件事
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void generateFuture() throws InterruptedException, ExecutionException {
		//返回结果
		CompletableFuture<Void> completableFuture1 = CompletableFuture.runAsync(() -> {
			System.out.println(first_thing);
		});

		//不返回结果
		CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {
			System.out.println(second_thing);
			return second_thing_return;
		});

		Void void1 = completableFuture1.get();
		System.out.println("run result=" + void1 + "\r\n" + "supply result=" + completableFuture2.get());

	}

	/**
	 * 做一件事,并捕捉异常
	 * 
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static void exceptionFuture() throws InterruptedException, ExecutionException {
		CompletableFuture<String> exceptionally = CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).exceptionally(ex -> ex.getMessage());

		System.out.println(exceptionally.get() + "");
	}

	/**
	 * 做一件事,在其结果上处理,生成新的结果
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void thenApply() throws InterruptedException, ExecutionException {
		CompletableFuture<String> thenApply = CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).thenApply(msg -> msg + "%%");

		System.out.println(thenApply.get() + "");
	}

	/**
	 * 第一件事完成后做第二件事情(接受第一件事结果),不返回结果
	 */
	public static void thenAccept() {
		CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).thenAccept(msg -> {
			System.out.println(second_thing + ",get msg=" + msg);
		});
	}

	/**
	 * 第一件事完成后做第二件事(两件事无关系)
	 */
	public static void thenRun() {
		CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).thenRun(() -> {
			System.out.println("second thing...");
		});
	}

	/**
	 * 第一件事完成后做第二件事(接受第一件事结果,并捕捉第一件事产生的异常)
	 */
	public static void whenComplete() {
		CompletableFuture.runAsync(() -> {
			System.out.println(first_thing);
		}).whenComplete((s, ex) -> {
			System.out.println(second_thing + "get msg=" + s + ",exception=" + ex);
		});

		CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).whenComplete((s, ex) -> {
			System.out.println(second_thing + "get msg=" + s + ",exception=" + ex);
		});
	}

	/**
	 * 第一件事完成后做第二件事情(接受第一件事结果),返回结果
	 * 
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static void thenCompose() throws InterruptedException, ExecutionException {
		CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		});

		CompletableFuture<Object> completableFuture2 = completableFuture1
				.thenCompose(msg -> CompletableFuture.supplyAsync(() -> {
					System.out.println(second_thing + ",get msg=" + msg);
					return second_thing_return;
				}));

		System.out.println(completableFuture2.get() + "");
	}

	/**
	 * 两件事情完成后做第三件事情(接受两件事情的结果),返回结果
	 * 
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */
	public static void thenAcceptBoth() throws InterruptedException, ExecutionException {
		CompletableFuture<Void> thenAcceptBoth = CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		}).thenAcceptBoth(CompletableFuture.supplyAsync(() -> {
			System.out.println(second_thing);
			return second_thing_return;
		}), (msg1, msg2) -> {
			System.out.println(third_thing + ",get msg=" + msg1 + ",msg2=" + msg2);
		});
		System.out.println(thenAcceptBoth.get() + "");
	}

	/**
	 * 两件事情完成后做第三件事请(与前两件事无关系),不返回结果
	 */
	public static void runAfterBoth() {
		CompletableFuture.runAsync(() -> {
			System.out.println(first_thing);
		}).runAfterBoth(CompletableFuture.supplyAsync(() -> {
			System.out.println(second_thing);
			return second_thing_return;
		}), () -> {
			System.out.println(third_thing);
		});
	}

	/**
	 * 两件事情完成后做第三件事请,返回结果
	 * 
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static void thenCombine() throws InterruptedException, ExecutionException {
		CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
			System.out.println(first_thing);
			return first_thing_return;
		});

		CompletableFuture<String> thenCombine = completableFuture1.thenCombine(CompletableFuture.supplyAsync(() -> {
			System.out.println(second_thing);
			return second_thing_return;
		}), (f1, f2) -> {
			System.out.println(third_thing);

			return third_thing_return;
		});

		System.out.println(thenCombine.get() + "");
	}

}
   
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值