Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ThreadLocal | Multithreading Best Practices
Multithreading in Java
course content

Contenido del Curso

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

ThreadLocal

ThreadLocal enables you to avoid multithreading-related mechanisms like synchronization, as data from ThreadLocal is accessible only within a single thread.

Imagine a banking application where each client has their own account. If multiple clients are using the application simultaneously, their accounts should remain separate. By using ThreadLocal, each thread (client) gets its own instance of data (account), ensuring that it does not interfere with other threads.

How to use ThreadLocal

ThreadLocal provides a get() method and a set() method, which are self-explanatory.

get() - retrieves the value from the ThreadLocal variable.

set() - set the value to ThreadLocal variable.

Let's create a program to compare a simple variable with ThreadLocal.

Each thread will have its own distinct threadLocalCounter, while the sharedCounter value will be changed and shared among all threads. The program's output will demonstrate that threadLocalCounter values are unique for each thread, whereas sharedCounter will reflect a common result for all threads, which might vary depending on the order in which the threads execute.

java

ThreadLocalComparison

java

CounterTask

And after this program works for us, we get the result:

As you can see, the sharedCounter variable is incremented across different threads, whereas the threadLocalCounter variable maintains the same value in each thread, because each thread has its own threadLocalCounter instance that is independent of other threads.

As you might have observed, the code includes a static method ThreadLocal.withInitial() that sets an initial value for the variable when it is created.

The remove() method in the ThreadLocal class is used to remove the value associated with the current thread.

java

ThreadLocalTask

java

ThreadLocalRemoveExample

As a result of the program execution:

The program assigns a unique value to ThreadLocal for each thread and displays this value before and after the remove() method is invoked. After removing the value with remove(), ThreadLocal returns null on subsequent calls to get().

Note

Data that are in ThreadLocal are not deleted themselves and they must be explicitly deleted!

Why do I Need to Free a Value?

Releasing a value is crucial to prevent memory leaks. ThreadLocal keeps values linked to threads in a special map that is bound to threads. If a value is not removed, it stays in the map even after the thread's usage is complete, leading to unnecessary objects remaining in memory and ultimately resulting in memory leaks.

1. What is the main problem that using ThreadLocal solves?
2. What is the initial value of a ThreadLocal variable when it is created using the withInitial() method?
3. What happens if you do not call the remove() method for a ThreadLocal variable in long-lived threads?

What is the main problem that using ThreadLocal solves?

Selecciona la respuesta correcta

What is the initial value of a ThreadLocal variable when it is created using the withInitial() method?

Selecciona la respuesta correcta

What happens if you do not call the remove() method for a ThreadLocal variable in long-lived threads?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 4. Capítulo 4
We're sorry to hear that something went wrong. What happened?
some-alt