8000 another simple demo for CompletableFuture class · fishercoder1534/RandomJava@3e3882a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e3882a

Browse files
another simple demo for CompletableFuture class
1 parent 8658182 commit 3e3882a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package multithread.completablefutureexamples;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.CompletableFuture;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Future;
10+
11+
import static multithread.completablefutureexamples.CompletableFutureDemo2.WorkerPool.TIME_FOR_A_COMPUTATION_JOB_IN_MS;
12+
13+
public class CompletableFutureDemo2 {
14+
15+
private static final int POOL_SIZE = 10;
16+
private static final int NUMBER_OF_COMPUTATION_JOBS = 20;
17+
18+
public static void main(String... args) throws ExecutionException, InterruptedException {
19+
System.out.println("Program started.");
20+
long start = System.currentTimeMillis();
21+
WorkerPool workerPool = new WorkerPool(POOL_SIZE);
22+
int finalResult = 0;
23+
List<Future<String>> futureList = new ArrayList<>();
24+
for (int i = 1; i <= NUMBER_OF_COMPUTATION_JOBS; i++) {
25+
Future<String> completableFuture = workerPool.executeAsync(i);
26+
futureList.add(completableFuture);
27+
}
28+
for (Future<String> future : futureList) {
29+
String result = future.get();
30+
finalResult += Integer.parseInt(result);
31+
}
32+
long end = System.currentTimeMillis();
33+
System.out.println("It took " + (end - start) / 1000
34+
+ " seconds to complete computation, final result: " + finalResult
35+
+ ", a total of " + NUMBER_OF_COMPUTATION_JOBS + " computation jobs "
36+
+ "have been completed, total pool worker size is: " + POOL_SIZE
37+
+ ", and each job took " + TIME_FOR_A_COMPUTATION_JOB_IN_MS / 1000 + " second(s)."
38+
);
39+
System.out.println("Program ended.");
40+
}
41+
42+
static class WorkerPool {
43+
static final long TIME_FOR_A_COMPUTATION_JOB_IN_MS = 1000l;
44+
int poolSize;
45+
ExecutorService executorService;
46+
47+
public WorkerPool(int poolSize) {
48+
this.poolSize = poolSize;
49+
this.executorService = Executors.newFixedThreadPool(poolSize);
50+
}
51+
52+
public Future<String> executeAsync(int input) {
53+
final CompletableFuture completableFuture = new CompletableFuture<>();
54+
this.executorService.submit(() -> doWork(completableFuture, input));
55+
return completableFuture;
56+
}
57+
58+
private void doWork(CompletableFuture completableFuture, int input) {
59+
int result = 0;
60+
try {
61+
System.out.println(Thread.currentThread().getName() + " is doing some real work now that'll take 1 second to complete.");
62+
Thread.sleep(TIME_FOR_A_COMPUTATION_JOB_IN_MS);
63+
result += input * 2;
64+
} catch (InterruptedException e) {
65+
throw new RuntimeException(e);
66+
}
67+
completableFuture.complete("" + result);
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)
0