CompletableFuture的使用,单个CompletableFuture的处理

CompletableFuture是Future和CompletionStage的一个实现。它可以通过回调的方式处理线程执行结果,也提供了转换和组合多个CompletableFuture 的方法。

CompletableFuture实例化

一共有四种实例化方法,runAsync是不带返回值的,supplyAsync是有返回值的。
第二个参数可选,表示是用哪一种方式的线程池。默认情况下使用的线程池为:ForkJoinPool.commonPool()

	CompletableFuture<Void> cf1 = CompletableFuture.runAsync(new Runnable() {
		@Override
		public void run() {
		}
	});
	
	CompletableFuture<Void> cf2 = CompletableFuture.runAsync(new Runnable() {
		@Override
		public void run() {
		}
	}, Executors.newFixedThreadPool(10));

	CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(new Supplier<String>() {
		@Override
		public String get() {
			return "";
		}
	});
	
	CompletableFuture<String> cf4 = CompletableFuture.supplyAsync(new Supplier<String>() {
		@Override
		public String get() {
			return "";
		}
	} , Executors.newFixedThreadPool(10));

获取线程执行结果,使用的方法为get()

String res = future.get();

返回类型与定义对象使用的泛型相同。

CompletableFuture中的方法

thenApply
该方法使用的参数为Function<A,B>第一个泛型为输入参数的类型,第二个参数为输出参数的类型。该方法返回类型为CompletableFuture。

	private static void thenApply() throws Exception {
		CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
			long result = new Random().nextInt(100);
			System.out.println("result1=" + result);
			return result;
		}).thenApply((t) -> {
			long result = t * 5;
			System.out.println("result2=" + result);
			return result;
		}).thenApply(new Function<Long, String>() {
			@Override
			public String apply(Long t) {
				return null;
			}
		});

		String result = future.get();
		System.out.println(result);
	}

thenAccept
该方法与上面的方法最大的区别在于参数为Consumer<A> 只接受上级方法返回参数,不对处理结果进行返回,是消费式的。

	public static void thenAccept() throws Exception {
		CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
			return new Random().nextInt(10);
		}).thenAccept(integer -> {
			System.out.println(integer);
		});
		future.get();
	}

thenRun
该方法在上一步方法执行之后执行。没有输入参数和输出参数。

	public static void thenRun() throws Exception {
		CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
			return new Random().nextInt(10);
		}).thenRun(() -> {
			System.out.println("thenRun ...");
		});
		future.get();
	}

handle
用于方法执行后执行,可以接受上级处理结果和上级方法异常。

	public static void handle() throws Exception {
		CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
			int i = 10 / 0;
			return new Random().nextInt(10);
		}).handle((param, throwable) -> {
			int result = -1;
			if (throwable == null) {
				result = param * 2;
			} else {
				System.out.println(throwable.getMessage());
			}
			return result;
		});
		System.out.println(future.get());
	}

exceptionally与whenComplete
whenComplete对执行结果进行处理,不影响get方法获取最初的返回值。当发生异常后exceptionally方法会执行。对异常进行处理。

	public static void whenCompleteWithSupply() throws Exception {
		CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
			if (new Random().nextInt() % 2 >= 0) {
				System.out.println("异常。。。。");
				int i = 12 / 0;
			}
			System.out.println("ok。。。。");
			return "true result";
		}).exceptionally(new Function<Throwable, String>() {
			@Override
			public String apply(Throwable t) {
				System.out.println("false result");
				return "false result";
			}
		}).whenComplete((t, action) -> {
			System.out.println("上一步执行结果" + t);
		});

		System.out.println("获取结果"+future.get());
	}

end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值