JUC-5-CompletableFuture常见API

学习使用CompletableFuture的常用API

package com.cheng;

import org.junit.Test;

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

public class CompletableFutureApi {

	/**
	 *thenRun、thenAccept、thenApply方法区别
	 */
	@Test
	public void test9() {
		//thenRun的结果与上一步无关
		System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenRun(()->{}).join());
		//thenAccept 是消费型函数式接口
		System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenAccept(System.out::println).join());
		//thenApply 串行处理前面的结果 有返回值
		System.out.println(CompletableFuture.supplyAsync(() -> "resultA").thenApply(v-> v + " resultB").join());

	}


	/**
	 *thenAccept方法测试  该方法是消费型函数式接口,这里打印了一下结果
	 */
	@Test
	public void test8() {
		CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
			return 1;
		}).thenApply(integer -> {
			return integer +2;
		}).thenApply(integer -> {
			return integer + 3;
		}).thenAccept(System.out::println);

	}


	/**
	 * handle方法测试  其计算是串行化的  即使当前步骤有异常也可以继续往下计算
	 */
	@Test
	public void test7() throws ExecutionException, InterruptedException {
		CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
				System.out.println("第一步");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return 1;
		}).handle((integer, e) -> {
			int i = 10/0;
			System.out.println("第二步");
			return integer +2;
		}).handle((integer, e) -> {
			System.out.println("第三步");
			return integer + 3;
		}).whenComplete((value, exception) -> {
			if (exception == null){
				System.out.println("计算结果:" + value);
			}
		}).exceptionally(e -> {
			e.printStackTrace();
			System.out.println(e.getMessage());
			return null;
		});

		System.out.println(completableFuture.get());

	}


	/**
	 * thenApply方法测试  其计算是串行化的  但是由于存在依赖关系,当前步骤有异常的话就终止计算了
	 */
	@Test
	public void test6() throws ExecutionException, InterruptedException {
		CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
				System.out.println("第一步");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return 1;
		}).thenApply(integer -> {
			System.out.println("第二步");
			return integer +2;
		}).thenApply(integer -> {
			System.out.println("第三步");
			return integer + 3;
		}).whenComplete((value, exception) -> {
			if (exception == null){
				System.out.println("计算结果:" + value);
			}
		}).exceptionally(e -> {
			e.printStackTrace();
			System.out.println(e.getMessage());
			return null;
		});

		System.out.println(completableFuture.get());

	}



	/**
	 * complete(T value)方法测试
	 * 如果尚未完成,则将get()和相关方法返回的值设置为给定值
	 */
	@Test
	public void test5(){
		CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return "abc";
		});
		// complete()返回true表示打断  false表示未打断
		System.out.println(completableFuture.complete("completeValue") + "   " + completableFuture.join());
	}


	/**
	 * getNow(T valueIfAbsent)方法测试  表示立即要结果  如果完成了则返回正确的结果,否则返回传入的结果 如"xxx"
	 * 如果完成则返回结果值(或抛出任何遇到的异常),否则返回给定的valueIfAbsent。
	 */
	@Test
	public void test4(){
		CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return "abc";
		});
		System.out.println(completableFuture.getNow("xxx"));
	}



	/**
	 * join()方法测试  和get方法一样
	 */
	@Test
	public void test3() {
		CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return "abc";
		});
		System.out.println(completableFuture.join());
	}

	/**
	 * get(long timeout, TimeUnit unit)方法测试  表示等待timeout秒 超时则报异常
	 */
	@Test
	public void test2() throws ExecutionException, InterruptedException, TimeoutException {
		CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return "abc";
		});
		System.out.println(completableFuture.get(1, TimeUnit.SECONDS));
	}


	/**
	 * get()方法测试  表示获取结果
	 */
	@Test
	public void test1() throws ExecutionException, InterruptedException, TimeoutException {
		CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return "abc";
		});
		System.out.println(completableFuture.get());
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值