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
31 lines (24 loc) · 781 Bytes
/
FutureDemo.java
File metadata and controls
31 lines (24 loc) · 781 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
package futureDemo;
import java.util.concurrent.*;
public class FutureDemo {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
Future<String> future = executorService.submit(new GetString());
try {
Thread.currentThread().sleep(3000);
System.out.println("awake");
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class GetString implements Callable<String> {
@Override
public String call() throws Exception {
Thread.currentThread().sleep(100);
return "Hello Callable";
}
}