异步方法调用【其他模式】

public class AsyncMethodInvocation {
	/**
	 * Async Method Invocation【异步方法调用】
	 */
	@Test
	public void all() throws InterruptedException, ExecutionException {
		final String result = "async method invocation";
		// 1)可执行的异步任务呢
		final Callable<String> callable = ()->{
			// 2)异步任务的结果
			return result;
		};
		// 3)执行异步任务的线程池
		final ExecutorService executorService = Executors.newFixedThreadPool(1);
		final Future<String> future = executorService.submit(callable);
		// 4)可在将来读取异步任务的计算结果
		assertEquals(result, future.get());
		executorService.awaitTermination(2, TimeUnit.SECONDS);
	}
}

posted on 2019-01-02 20:39  竺旭东  阅读(111)  评论(0编辑  收藏  举报

导航