File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
src/main/java/concurrency Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1+ package concurrency .future ;
2+
3+ import java .util .concurrent .ExecutionException ;
4+ import java .util .concurrent .ExecutorService ;
5+ import java .util .concurrent .Executors ;
6+ import java .util .concurrent .Future ;
7+ import lombok .extern .slf4j .Slf4j ;
8+
9+ @ Slf4j
10+ public class FutureEx {
11+
12+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
13+ log .debug ("Enter" );
14+ final ExecutorService executorService = Executors .newCachedThreadPool ();
15+
16+ final long startTime = System .currentTimeMillis ();
17+ Future <String > future = executorService .submit (
18+ () -> {
19+ log .debug ("Processing task asynchronously" );
20+ try {
21+ Thread .sleep (2000 );
22+ } catch (InterruptedException e ) {
23+ throw new RuntimeException (e );
24+ }
25+
26+ return "AsyncResult" ;
27+ }
28+ );
29+
30+ log .debug ("Is the async task done? " + future .isDone ());
31+ log .debug ("Processing the other task" );
32+ Thread .sleep (2000 );
33+
34+ log .debug ("Is the async task done? " + future .isDone ());
35+ log .debug ("The async result is " + future .get ()); // blocking, wait the async result.
36+ log .debug ("Exit " + (System .currentTimeMillis () - startTime ));
37+ }
38+ }
Original file line number Diff line number Diff line change 11package concurrency .reactive ;
22
3- import java .util .concurrent .ExecutorService ;
43import java .util .concurrent .Executors ;
54import java .util .concurrent .ScheduledExecutorService ;
65import java .util .concurrent .TimeUnit ;
You can’t perform that action at this time.
0 commit comments