多线程CompletableFuture

1.消息处理,需要完成两件事情

public boolean expend(Object body) {
  

	AtomicBoolean flag = new AtomicBoolean(true);
	// 第一件事
	CompletableFuture<Void> f1 = CompletableFuture.runAsync(() -> {

		doThingOne(body);

	}, ThreadPool.pool).exceptionally((e) -> {
		flag.set(false);
		return null;
	});

	// 第二件事
	CompletableFuture<Void> f2 = CompletableFuture.runAsync(() -> {

		doThingTwo();
	}, ThreadPool.pool).exceptionally((e) -> {
		flag.set(false);
		return null;
	});

	CompletableFuture.allOf(f1, f2).join();

	return flag.get();
}

  

原文地址:https://www.cnblogs.com/hongyedeboke/p/14987995.html