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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章