1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Science & Technology
  6. >
  7. Java ExecutorService - Part 4 - Callable / Future

Java ExecutorService - Part 4 - Callable / Future

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: Defog Tech
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
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:00
In the previous videos for the executor service, we saw how to submit the tasks
0:05
which is nothing but a class which implements runnable
0:09
wherein you can define the number of steps to execute in a separate thread within the run method and how to
0:17
submit those tasks using the execute method
0:21
But there is a distinct use case that we did not see. We did not see what if the task is
0:27
implemented such that it wants to return a value. So suppose I want to return a
0:33
value from this run method there is no way I can do that. Say I want to return a
0:39
value of 3
0:40
the time. I cannot do that because the runnable interface, if you go inside the runnable interface,
0:46
you'll see there is only a single method called run and
0:50
the return type of that run method is void. So there is no way you can return a value.
0:56
Go back.
0:58
So there is another class called callable which is created exactly for this purpose.
1:03
So instead of runnable, if we implement a callable interface
1:08
which is this and you mention it is generics based so you mentioned which
1:13
kind of variable you are going to return so let's say here we are going to return
1:17
an integer right now intelliJ is telling me to implement the method and let's
1:24
delete this run method first now callable interface has this method
1:31
called call and based on what generics you mentioned here it will return that
1:37
particular value. Now we can very easily return a particular value. Now let's say
1:43
instead of always returning 3, let me do something fancy by saying return
1:48
always return a random number. Okay. But now if you see there is a compile-time
1:55
exception during the submission of the task. Right. Let's first just delete this
2:02
for loop. We don't want to do this hundred times. Let's say we want to do it
2:05
only once for now. This compile-time exception is because the execute method
2:11
expects a runnable task but instead now we have a callable task and there is a
2:17
separate method in the executor service to submit the callable tasks and the
2:23
method name is submit and as you can see you can submit even a runnable task
2:28
which works the same way as triggering the execute so there is no difference
2:33
there but if you want to submit a callable tasks then you always have to
2:37
use the submit. So now there is no compile time exception, we have submitted
2:40
the task. Now we have to accept the value of this method call, this integer value
2:48
which is being returned, we have to accept it in some
2:51
variable. So let's say that variable is future. The future type should have been
2:59
integer but it is not. It is of a type of class called future. As the name suggests
3:07
it's a placeholder for the value which will arrive sometime in the future and
3:14
based on how long your call operation takes. So let's say your call operation
3:18
here is let's say thread.sleep. I want to emulate that this operation takes a
3:24
long time so we want to sleep for three seconds here and after that calculate
3:29
the run, calculate the next random number. Now here for those three seconds you can
3:35
perform some other operations. You can say perform some operations I don't
3:40
know what but by the time the execute service is running your callable method
3:45
in a separate thread you can perform some other operations some unrelated operations here, right and
3:54
Then once those unrelated operations are finished it may take one second two second three seconds whatever after those
4:01
Operations are finished you can say future dot get
4:06
right and this
4:07
Will give you the actual integer result of that callable
4:12
task
4:14
Now if these operations finish very fast, let's say these operations finish within one
4:22
second.
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome