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

Java ExecutorService - Part 1 - Introduction

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 ---------------------------------- Complex concepts explained in short & simple manner. Topics include Java Concurrency, Spring Boot, Microservices, Distributed Systems etc. Feel free to ask any doubts in the comments. Also happy to take requests for new videos. Subscribe or explore the channel - https://youtube.com/defogtech New video added every weekend. Popular Videos ---------------------------------- What is an API Gateway - https://youtu.be/vHQqQBYJtLI Executor Service - https://youtu.be/6Oo-9Can3H8 Introduction to CompletableFuture - https://youtu.be/ImtZgX1nmr8 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 Java Concurrency Interview question - https://youtu.be/_RSAS-gIjGo
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 Java, it's always been easy to run a method or a piece of code asynchronously.
0:07
That is, if you have a main method which has a main thread, you can run your task in a
0:12
separate thread.
0:13
And you can do this very easily using a code similar to this.
0:17
So you can create a class, say for example, Task, which implements Runnable.
0:22
You can override its run method, put the code that you want to run asynchronously.
0:26
In this case, we are just doing the system.out.println() which will print the name of the thread that it is running in.
0:35
You can create a new thread and
0:37
with the instance of this particular task
0:40
and then just start the thread and the way to visualize this is there is a main
0:46
thread which performs its operation from top to down at certain point in time it
0:51
will do thread.start and the Java will create a thread named thread-0
0:57
this thread will do its own operations and once those operations are done Java
1:03
will kill that thread and the main thread will keep going. To extend this if
1:09
If you want to similarly run 10 tasks, you can have the same class task which implements runnable
1:16
But now you can create the threads in a for loop
1:20
So in this for loop you have i equal to 0 to 10
1:23
So it runs 10 times each time it will create a new thread with a new instance of task and will start that thread
1:32
and the way to visualize this is
1:34
very similar where Java will create threads called thread-0 till thread-9
1:41
denoted by T0 to T9 here and once the operations within the run method are
1:47
complete Java will kill those threads. To extend this even further what if you
1:53
want to run 1000 tasks asynchronously. The problem here is in Java one Java
2:01
thread corresponds to one operating system thread. That means if you run the
2:07
for loop thousand times it will create 1000 threads from T0 to T999 and
2:14
creating a thread in itself is an expensive operation. What you rather want
2:19
is you want a fixed number of threads say 10
2:22
threads, you want to create them up front, let's call it a pool, a pool of threads
2:28
that's why the name thread pool and let's submit thousand tasks to them and
2:33
then we want the threads to pick up those tasks, complete tasks, say for
2:39
example, T0 will pick one task called task 1, it will complete that task and
2:45
then immediately start with task 2. Similarly all the threads will pick up
2:49
the tasks and execute them. And the way to code this is very simple. You create a
2:56
new fixed thread pool using the static constructor or the static method of
3:02
executor's class. Here we are giving the number of threads as 10. It will give back
3:07
the instance variable for execute service. And now you use the same for loop
3:13
that you used earlier but here instead of starting a new thread you can just
3:18
submit or execute those tasks, new tasks, using the same service.
3:25
And the way to visualize this is that you have a for loop,
3:29
but within the for loop now, instead of creating new threads,
3:33
you're just creating new tasks and submitting them to the service.
3:37
And within the service, which is the thread pool executor,
3:41
it internally uses a blocking queue.
3:43
In that queue, it keeps storing all the tasks that you have submitted.
3:48
all the 10 threads which is T0 to T9, all of them will perform these same two
3:55
steps. The two steps are fetch the next task from the queue and execute it and
4:02
since all 10 threads are temporary
4:04
to take the task from the queue at the same time, that is concurrently, you want
4:10
the queue to be able to handle concurrent operations. So you want a queue
4:15
which is thread safe and that is why a thread pool uses the blocking queue
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome