WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Concurrency · Advanced · question 48 of 100

What is a ThreadLocal variable in Java and how is it used?

📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

In Java, a ThreadLocal variable is a variable that is local to a thread. This means that each thread has its own copy of the variable, and changes made to the variable by one thread do not affect the value of the variable in other threads. ThreadLocal variables are often used to store data that is specific to a thread, such as thread-specific user credentials, session information, or database connections.

The ThreadLocal class in Java provides a way to create thread-local variables. To use ThreadLocal, you create an instance of the class and store it as a static or instance variable in your code. You can then use the get() and set() methods of the ThreadLocal object to retrieve or set the value of the variable in the current thread.

Here is an example of how to use ThreadLocal in Java:

public class MyThreadLocalExample {
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        threadLocal.set("hello");
        System.out.println(threadLocal.get());
        new Thread(() -> {
            threadLocal.set("world");
            System.out.println(threadLocal.get());
        }).start();
        System.out.println(threadLocal.get());
    }
}

In this example, we create a ThreadLocal variable named threadLocal that stores a string value. We set the value of the variable to "hello" in the main thread and print it out. We then create a new thread that sets the value of the variable to "world" and prints it out. Finally, we print out the value of the variable in the main thread again.

When we run this program, we get the following output:

hello
null
world

As you can see, the value of the ThreadLocal variable is different in each thread. In the main thread, the value is "hello", while in the new thread, the value is "world". When we access the variable again in the main thread, the value is null because we never set it in that thread.

ThreadLocal variables can be useful in a variety of situations where you need to store thread-specific data. However, it’s important to use them carefully to avoid creating memory leaks or other issues. In particular, you should always make sure to remove the value of a ThreadLocal variable when you’re done with it to avoid keeping unnecessary references to objects that may never be garbage collected.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Concurrency interview — then scores it.
📞 Practice Java Concurrency — free 15 min
📕 Buy this interview preparation book: 100 Java Concurrency questions & answers — PDF + EPUB for $5

All 100 Java Concurrency questions · All topics