Java ExecutorService - Part 1 - Introduction
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
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
In Java, it's always been easy to run a method or a piece of code asynchronously.
That is, if you have a main method which has a main thread, you can run your task in a
separate thread.
And you can do this very easily using a code similar to this.
So you can create a class, say for example, Task, which implements Runnable.
You can override its run method, put the code that you want to run asynchronously.
In this case, we are just doing the system.out.println() which will print the name of the thread that it is running in.
You can create a new thread and
with the instance of this particular task
and then just start the thread and the way to visualize this is there is a main
thread which performs its operation from top to down at certain point in time it
will do thread.start and the Java will create a thread named thread-0
this thread will do its own operations and once those operations are done Java
will kill that thread and the main thread will keep going. To extend this if
If you want to similarly run 10 tasks, you can have the same class task which implements runnable
But now you can create the threads in a for loop
So in this for loop you have i equal to 0 to 10
So it runs 10 times each time it will create a new thread with a new instance of task and will start that thread
and the way to visualize this is
very similar where Java will create threads called thread-0 till thread-9
denoted by T0 to T9 here and once the operations within the run method are
complete Java will kill those threads. To extend this even further what if you
want to run 1000 tasks asynchronously. The problem here is in Java one Java
thread corresponds to one operating system thread. That means if you run the
for loop thousand times it will create 1000 threads from T0 to T999 and
creating a thread in itself is an expensive operation. What you rather want
is you want a fixed number of threads say 10
threads, you want to create them up front, let's call it a pool, a pool of threads
that's why the name thread pool and let's submit thousand tasks to them and
then we want the threads to pick up those tasks, complete tasks, say for
example, T0 will pick one task called task 1, it will complete that task and
then immediately start with task 2. Similarly all the threads will pick up
the tasks and execute them. And the way to code this is very simple. You create a
new fixed thread pool using the static constructor or the static method of
executor's class. Here we are giving the number of threads as 10. It will give back
the instance variable for execute service. And now you use the same for loop
that you used earlier but here instead of starting a new thread you can just
submit or execute those tasks, new tasks, using the same service.
And the way to visualize this is that you have a for loop,
but within the for loop now, instead of creating new threads,
you're just creating new tasks and submitting them to the service.
And within the service, which is the thread pool executor,
it internally uses a blocking queue.
In that queue, it keeps storing all the tasks that you have submitted.
all the 10 threads which is T0 to T9, all of them will perform these same two
steps. The two steps are fetch the next task from the queue and execute it and
since all 10 threads are temporary
to take the task from the queue at the same time, that is concurrently, you want
the queue to be able to handle concurrent operations. So you want a queue
which is thread safe and that is why a thread pool uses the blocking queue