forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureIsDoneEx.java
More file actions
33 lines (22 loc) · 843 Bytes
/
FutureIsDoneEx.java
File metadata and controls
33 lines (22 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.zetcode;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class FutureIsDoneEx {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> future = executorService.submit(() -> {
Thread.sleep(2000);
return "result from callable";
});
while (!future.isDone()) {
System.out.println("working on task...");
Thread.sleep(400);
}
System.out.println("task completed");
var result = future.get();
System.out.println(result);
executorService.shutdown();
}
}