Skip to content

Commit 539aebc

Browse files
CompletableFuture多任务异步的简单例子
1 parent f8460dc commit 539aebc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java8.completefuture;
2+
3+
import java.util.concurrent.*;
4+
5+
public class OfAllGetSimpleDemo {
6+
public static void main(String[] args) throws Exception {
7+
allOfTest();
8+
}
9+
10+
public static void allOfTest() throws Exception {
11+
ExecutorService executorService = Executors.newCachedThreadPool();
12+
CompletableFuture<Void> cf1 = CompletableFuture.runAsync(
13+
() -> System.out.println("cf1 ok."), executorService);
14+
CompletableFuture<Void> cf2 = CompletableFuture.runAsync(
15+
() -> System.out.println("cf2 ok."), executorService);
16+
17+
//将两个任务组装成一个新的任务,总共的超时时间为2s
18+
CompletableFuture.allOf(cf1, cf2).get(2, TimeUnit.SECONDS);
19+
}
20+
}

0 commit comments

Comments
 (0)