1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Education
  6. >
  7. ThreadLocal in Java

ThreadLocal in Java

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: Defog Tech
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
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
Let's start with the use case.
0:02
So let's say we have a class called user service, which has a method called birthdate.
0:07
This method given a user ID will return the birth date of that
0:11
user in the form of a string.
0:13
To do that, it'll first get the birth date from the database using the user ID.
0:18
It'll create an object of this class called simple date format.
0:21
And using this object, it will format the date and convert it into a string
0:26
and return that particular string.
0:28
In the main method, we have two threads.
0:31
Both of these threads call this method of birthdate
0:33
with a particular user ID
0:35
and whatever is the return value,
0:38
it just prints that in the console.
0:40
So visually we have two threads.
0:42
Both of them are calling a task, which is nothing but a runnable.
0:46
And when they call the birth date method, both these tasks will internally create
0:51
this object of simple date format.
0:53
Now, if we change the code a little bit, so instead of creating just two threads,
0:58
let's have a for loop and create 10 threads, all doing the same thing.
1:02
So in this case, we have 10 threads.
1:04
Each of the thread is using a different user ID, but essentially calling the same method.
1:08
So in this case, we now have 10 threads.
1:12
All of them are running their own runnable tasks and internally, all of them will
1:17
create their own date format objects.
1:21
And if we extend the code even further, let's say now we want to do the
1:24
same thing, but for thousand users.
1:27
In this case, it's impractical to create 1000 threads that will consume a lot of
1:32
memory and that's why we can use thread pool in Java instead.
1:36
So here we are creating a fixed thread pool of size 10.
1:40
That means there are only 10 threads.
1:42
We have a for loop, which will run thousand times, and we are going to
1:46
submit 1000 tasks each for a different user ID, and these tasks will be run
1:51
by the thread pool using the 10 threads.
1:54
So in this case, we have a thread pool of 10 threads always, and we have 1000 tasks.
2:01
So internally, all these tasks will create their own date format objects.
2:05
So for thousand tasks, we have thousand date format objects.
2:09
But this is slightly inefficient because these date formats can be reused across all these tasks.
2:16
So one way to reuse it is we will remove the creation of simple date format from the birthdate method.
2:24
Instead, we'll have a global simple date format object and we'll keep the rest of the code as is.
2:30
So this time we have multiple threads. We are showing only two threads here
2:34
Let's say we have multiple threads and all of them will access the same single global object of simple date format
2:42
The problem with this scenario is the simple date format is not a thread safe class
2:48
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
2:56
One way to resolve this issue is to have locks
2:58
So if we have a lock here, that means only one thread will be allowed to access the simple
3:04
date format at a time.
3:05
So now we have introduced synchronization and because of that, our class and the code
3:10
will slow down a lot.
3:12
So we cannot have one object being created per task that consumes a lot of memory and
3:18
we cannot have a global object because it is not thread safe.
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome