ThreadLocal in Java
Use ThreadLocal for thread-safety, memory efficiency and for per-thread context storage.
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
Let's start with the use case.
So let's say we have a class called user service, which has a method called birthdate.
This method given a user ID will return the birth date of that
user in the form of a string.
To do that, it'll first get the birth date from the database using the user ID.
It'll create an object of this class called simple date format.
And using this object, it will format the date and convert it into a string
and return that particular string.
In the main method, we have two threads.
Both of these threads call this method of birthdate
with a particular user ID
and whatever is the return value,
it just prints that in the console.
So visually we have two threads.
Both of them are calling a task, which is nothing but a runnable.
And when they call the birth date method, both these tasks will internally create
this object of simple date format.
Now, if we change the code a little bit, so instead of creating just two threads,
let's have a for loop and create 10 threads, all doing the same thing.
So in this case, we have 10 threads.
Each of the thread is using a different user ID, but essentially calling the same method.
So in this case, we now have 10 threads.
All of them are running their own runnable tasks and internally, all of them will
create their own date format objects.
And if we extend the code even further, let's say now we want to do the
same thing, but for thousand users.
In this case, it's impractical to create 1000 threads that will consume a lot of
memory and that's why we can use thread pool in Java instead.
So here we are creating a fixed thread pool of size 10.
That means there are only 10 threads.
We have a for loop, which will run thousand times, and we are going to
submit 1000 tasks each for a different user ID, and these tasks will be run
by the thread pool using the 10 threads.
So in this case, we have a thread pool of 10 threads always, and we have 1000 tasks.
So internally, all these tasks will create their own date format objects.
So for thousand tasks, we have thousand date format objects.
But this is slightly inefficient because these date formats can be reused across all these tasks.
So one way to reuse it is we will remove the creation of simple date format from the birthdate method.
Instead, we'll have a global simple date format object and we'll keep the rest of the code as is.
So this time we have multiple threads. We are showing only two threads here
Let's say we have multiple threads and all of them will access the same single global object of simple date format
The problem with this scenario is the simple date format is not a thread safe class
That means if two or more threads try to access this object at the same time, it's going to have an data integrity issue
One way to resolve this issue is to have locks
So if we have a lock here, that means only one thread will be allowed to access the simple
date format at a time.
So now we have introduced synchronization and because of that, our class and the code
will slow down a lot.
So we cannot have one object being created per task that consumes a lot of memory and
we cannot have a global object because it is not thread safe.