Skip to content

Commit 0f35b19

Browse files
author
Rajeev Kumar Singh
committed
Future cancel
1 parent b855c63 commit 0f35b19

File tree

6 files changed

+7
-6
lines changed

6 files changed

+7
-6
lines changed
Binary file not shown.
Binary file not shown.

java-callable-and-future-examples/src/FutureCancelExample.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
1919
while(!future.isDone()) {
2020
System.out.println("Task is still not done...");
2121
Thread.sleep(200);
22-
double elapsedTimeInSec = (System.nanoTime() - startTime)/1000000000.0;
22+
double elapsedTimeInSec = (System.nanoTime() - startTime) / 1000000000.0;
2323

24-
if(elapsedTimeInSec > 1) {
24+
if (elapsedTimeInSec > 1) {
2525
// cancel future if the elapsed time is more than one second
2626
future.cancel(true);
2727
}
2828
}
2929

3030
// Check if future is cancelled before retrieving the result
31-
// if(!future.isCancelled()) {
31+
if(!future.isCancelled()) {
3232
System.out.println("Task completed! Retrieving the result");
3333
// Future.get() blocks until the result is available
3434
String result = future.get();
3535
System.out.println(result);
36-
// } else {
37-
// System.out.println("Task was cancelled");
38-
// }
36+
} else {
37+
System.out.println("Task was cancelled");
38+
}
39+
3940
executorService.shutdown();
4041
}
4142

Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy