File tree Expand file tree Collapse file tree 5 files changed +164
-0
lines changed
java-callables-and-futures/src Expand file tree Collapse file tree 5 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .concurrent .*;
2
+
3
+ /**
4
+ * Created by rajeevkumarsingh on 11/05/17.
5
+ */
6
+ public class FutureAndCallableExample {
7
+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
8
+ ExecutorService executorService = Executors .newSingleThreadExecutor ();
9
+
10
+ Callable <String > callable = () -> {
11
+ // Perform some computation
12
+ Thread .sleep (2000 );
13
+ return "Hello from Callable" ;
14
+ };
15
+
16
+ Future <String > future = executorService .submit (callable );
17
+ // This line executes immediately
18
+ System .out .println ("Do something else while callable is getting executed" );
19
+
20
+ System .out .println ("Retrieve the result of the future" );
21
+ // Future.get() blocks until the result is available
22
+ String result = future .get ();
23
+ System .out .println (result );
24
+
25
+ executorService .shutdown ();
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .concurrent .*;
2
+
3
+ /**
4
+ * Created by rajeevkumarsingh on 10/05/17.
5
+ */
6
+ public class FutureCancelExample {
7
+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
8
+ ExecutorService executorService = Executors .newSingleThreadExecutor ();
9
+
10
+ Callable <String > callable = () -> {
11
+ // Perform some computation
12
+ Thread .sleep (2000 );
13
+ return "Hello from Callable" ;
14
+ };
15
+
16
+ long startTime = System .nanoTime ();
17
+ Future <String > future = executorService .submit (callable );
18
+
19
+ while (!future .isDone ()) {
20
+ System .out .println ("Task is still not done..." );
21
+ Thread .sleep (200 );
22
+ double elapsedTimeInSec = (System .nanoTime () - startTime )/1000000000.0 ;
23
+
24
+ if (elapsedTimeInSec > 1 ) {
25
+ // cancel future if the elapsed time is more than one second
26
+ future .cancel (true );
27
+ }
28
+ }
29
+
30
+ // Check if future is cancelled before retrieving the result
31
+ if (!future .isCancelled ()) {
32
+ System .out .println ("Task completed! Retrieving the result" );
33
+ // Future.get() blocks until the result is available
34
+ String result = future .get ();
35
+ System .out .println (result );
36
+ } else {
37
+ System .out .println ("Task was cancelled" );
38
+ }
39
+ executorService .shutdown ();
40
+ }
41
+
42
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .concurrent .*;
2
+
3
+ /**
4
+ * Created by rajeevkumarsingh on 11/05/17.
5
+ */
6
+ public class FutureIsDoneExample {
7
+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
8
+ ExecutorService executorService = Executors .newSingleThreadExecutor ();
9
+
10
+ Future <String > future = executorService .submit (() -> {
11
+ Thread .sleep (2000 );
12
+ return "Hello from Callable" ;
13
+ });
14
+
15
+ while (!future .isDone ()) {
16
+ System .out .println ("Task is still not done..." );
17
+ Thread .sleep (200 );
18
+ }
19
+
20
+ System .out .println ("Task completed! Retrieving the result" );
21
+ String result = future .get ();
22
+ System .out .println (result );
23
+
24
+ executorService .shutdown ();
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+ import java .util .List ;
3
+ import java .util .concurrent .*;
4
+
5
+ /**
6
+ * Created by rajeevkumarsingh on 10/05/17.
7
+ */
8
+ public class InvokeAllExample {
9
+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
10
+ ExecutorService executorService = Executors .newFixedThreadPool (5 );
11
+
12
+ Callable <String > task1 = () -> {
13
+ Thread .sleep (2000 );
14
+ return "Result of Task1" ;
15
+ };
16
+
17
+ Callable <String > task2 = () -> {
18
+ Thread .sleep (1000 );
19
+ return "Result of Task2" ;
20
+ };
21
+
22
+ Callable <String > task3 = () -> {
23
+ Thread .sleep (5000 );
24
+ return "Result of Task3" ;
25
+ };
26
+
27
+ List <Callable <String >> taskList = Arrays .asList (task1 , task2 , task3 );
28
+
29
+ List <Future <String >> futures = executorService .invokeAll (taskList );
30
+
31
+ for (Future <String > future : futures ) {
32
+ // The result is printed only after all the futures are complete. (i.e. after 5 seconds)
33
+ System .out .println (future .get ());
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Arrays ;
2
+ import java .util .List ;
3
+ import java .util .concurrent .*;
4
+
5
+ /**
6
+ * Created by rajeevkumarsingh on 10/05/17.
7
+ */
8
+ public class InvokeAnyExample {
9
+ public static void main (String [] args ) throws InterruptedException , ExecutionException {
10
+ ExecutorService executorService = Executors .newFixedThreadPool (5 );
11
+
12
+ Callable <String > task1 = () -> {
13
+ Thread .sleep (2000 );
14
+ return "Result of Task1" ;
15
+ };
16
+
17
+ Callable <String > task2 = () -> {
18
+ Thread .sleep (1000 );
19
+ return "Result of Task2" ;
20
+ };
21
+
22
+ Callable <String > task3 = () -> {
23
+ Thread .sleep (5000 );
24
+ return "Result of Task3" ;
25
+ };
26
+
27
+ // Returns the result of the fastest callable. (task2 in this case)
28
+ String result = executorService .invokeAny (Arrays .asList (task1 , task2 , task3 ));
29
+
30
+ System .out .println (result );
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments