forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfutureDemo.java
More file actions
34 lines (25 loc) · 813 Bytes
/
futureDemo.java
File metadata and controls
34 lines (25 loc) · 813 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
34
package multithreading;
import java.util.concurrent.*;
public class futureDemo {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future future = executorService.submit(new PrintA());
try {
while (!future.isDone()) {
}
String result = (String) future.get();
System.out.println("isDone:" + future.isDone());
System.out.println("result:" + result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class PrintA implements Callable<String> {
@Override
public String call() throws Exception {
return "Hello Callable";
}
}