Java ExecutorService - Part 4 - Callable / Future
Learn how to parallelize your tasks and operations easily in Java without cooking up your own Threads.
Part 1: Introduction - https://www.youtube.com/watch?v=6Oo-9Can3H8
Part 2: Type of Pools - https://www.youtube.com/watch?v=sIkG0X4fqs4
Part 3: Constructor / LifeCycle - https://www.youtube.com/watch?v=Dma_NmOrp1c
Part 4: Callable/Future - https://www.youtube.com/watch?v=NEZ2ASoP_nY
Channel
----------------------------------
Master difficult programming concepts in few minutes. I try to explain difficult concepts like Java concurrency in simple to understand manner using animations and small code snippets. Explore videos on topics like Spring Boot, Cloud Foundry, Java 8 (with more coming soon). I am happy to clarify your doubts. Ask me anything in the comments. I am also happy to take requests for new videos.
New video added every Sunday.
Subscribe or explore the channel - http://bit.ly/defog_tech
Playlists
----------------------------------
Java Executor Service - http://bit.ly/exec_srvc
Java Concurrency - http://bit.ly/java_crncy
Spring Boot 2.0 - http://bit.ly/spr_boot2
Java 8 - http://bit.ly/java_8-11
Intellij IDEA Shortcuts - http://bit.ly/i_idea
Popular Videos
----------------------------------
Executor Service - https://youtu.be/6Oo-9Can3H8
Introduction to CompletableFuture - https://youtu.be/ImtZgX1nmr8
Understand how ForkJoinPool works - https://youtu.be/5wgZYyvIVJk
Java Memory Model in 10 minutes - https://youtu.be/Z4hMFBvCDV4
Volatile vs Atomic - https://youtu.be/WH5UvQJizH0
What is Spring Webflux - https://youtu.be/M3jNn3HMeWg
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
In the previous videos for the executor service, we saw how to submit the tasks
which is nothing but a class which implements runnable
wherein you can define the number of steps to execute in a separate thread within the run method and how to
submit those tasks using the execute method
But there is a distinct use case that we did not see. We did not see what if the task is
implemented such that it wants to return a value. So suppose I want to return a
value from this run method there is no way I can do that. Say I want to return a
value of 3
the time. I cannot do that because the runnable interface, if you go inside the runnable interface,
you'll see there is only a single method called run and
the return type of that run method is void. So there is no way you can return a value.
Go back.
So there is another class called callable which is created exactly for this purpose.
So instead of runnable, if we implement a callable interface
which is this and you mention it is generics based so you mentioned which
kind of variable you are going to return so let's say here we are going to return
an integer right now intelliJ is telling me to implement the method and let's
delete this run method first now callable interface has this method
called call and based on what generics you mentioned here it will return that
particular value. Now we can very easily return a particular value. Now let's say
instead of always returning 3, let me do something fancy by saying return
always return a random number. Okay. But now if you see there is a compile-time
exception during the submission of the task. Right. Let's first just delete this
for loop. We don't want to do this hundred times. Let's say we want to do it
only once for now. This compile-time exception is because the execute method
expects a runnable task but instead now we have a callable task and there is a
separate method in the executor service to submit the callable tasks and the
method name is submit and as you can see you can submit even a runnable task
which works the same way as triggering the execute so there is no difference
there but if you want to submit a callable tasks then you always have to
use the submit. So now there is no compile time exception, we have submitted
the task. Now we have to accept the value of this method call, this integer value
which is being returned, we have to accept it in some
variable. So let's say that variable is future. The future type should have been
integer but it is not. It is of a type of class called future. As the name suggests
it's a placeholder for the value which will arrive sometime in the future and
based on how long your call operation takes. So let's say your call operation
here is let's say thread.sleep. I want to emulate that this operation takes a
long time so we want to sleep for three seconds here and after that calculate
the run, calculate the next random number. Now here for those three seconds you can
perform some other operations. You can say perform some operations I don't
know what but by the time the execute service is running your callable method
in a separate thread you can perform some other operations some unrelated operations here, right and
Then once those unrelated operations are finished it may take one second two second three seconds whatever after those
Operations are finished you can say future dot get
right and this
Will give you the actual integer result of that callable
task
Now if these operations finish very fast, let's say these operations finish within one
second.