Skip to content

Commit 3c6f3be

Browse files
committed
future, asynchronous processing
1 parent 81c3b83 commit 3c6f3be

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

src/main/java/concurrency/reactive/IntervalEx.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package concurrency.reactive;
22

3-
import java.util.concurrent.ExecutorService;
43
import java.util.concurrent.Executors;
54
import java.util.concurrent.ScheduledExecutorService;
65
import java.util.concurrent.TimeUnit;

0 commit comments

Comments
 (0)