WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Java Concurrency Β· Guru Β· question 89 of 100

What is the difference between the Java ThreadLocal and InheritableThreadLocal classes and how are they used?

πŸ“• Buy this interview preparation book: 100 Java Concurrency questions & answers β€” PDF + EPUB for $5

The Java ThreadLocal and InheritableThreadLocal classes are used to maintain thread-local variables in Java. A thread-local variable is a variable that is only accessible to the thread that created it, meaning that each thread has its own copy of the variable.

The difference between ThreadLocal and InheritableThreadLocal is that ThreadLocal is not inherited by child threads, whereas InheritableThreadLocal is inherited. This means that if a new thread is created by an existing thread, the new thread will have its own copy of a ThreadLocal variable, but it will share the same InheritableThreadLocal variable as its parent thread.

Here is an example to illustrate the difference between ThreadLocal and InheritableThreadLocal:

import java.util.concurrent.*;

public class ThreadLocalExample {
    private static final ThreadLocal<String> THREAD\_LOCAL = new ThreadLocal<>();
    private static final InheritableThreadLocal<String> INHERITABLE\_THREAD\_LOCAL = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        THREAD\_LOCAL.set("Hello, ThreadLocal!");
        INHERITABLE\_THREAD\_LOCAL.set("Hello, InheritableThreadLocal!");
        executor.submit(() -> {
            System.out.println("Thread 1: " + THREAD\_LOCAL.get());
            System.out.println("Thread 1: " + INHERITABLE\_THREAD\_LOCAL.get());
            THREAD\_LOCAL.set("Thread 1: modified");
            INHERITABLE\_THREAD\_LOCAL.set("Thread 1: modified");
            Thread.sleep(1000);
            System.out.println("Thread 1: " + THREAD\_LOCAL.get());
            System.out.println("Thread 1: " + INHERITABLE\_THREAD\_LOCAL.get());
        });
        executor.submit(() -> {
            System.out.println("Thread 2: " + THREAD\_LOCAL.get());
            System.out.println("Thread 2: " + INHERITABLE\_THREAD\_LOCAL.get());
            THREAD\_LOCAL.set("Thread 2: modified");
            INHERITABLE\_THREAD\_LOCAL.set("Thread 2: modified");
            Thread.sleep(1000);
            System.out.println("Thread 2: " + THREAD\_LOCAL.get());
            System.out.println("Thread 2: " + INHERITABLE\_THREAD\_LOCAL.get());
        });
        executor.shutdown();
    }
}

In this example, we create a ThreadLocal variable and an InheritableThreadLocal variable. We set their values to "Hello, ThreadLocal!" and "Hello, InheritableThreadLocal!", respectively.

We then create two threads and submit them to an ExecutorService. In each thread, we print the values of the two variables, modify their values, sleep for 1 second, and then print their values again.

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

Thread 1: Hello, ThreadLocal!
Thread 2: null
Thread 1: Hello, InheritableThreadLocal!
Thread 2: Hello, InheritableThreadLocal!
Thread 1: Thread 1: modified
Thread 2: null
Thread 1: Thread 1: modified
Thread 2: Thread 2: modified
Thread 1: Thread 1: modified
Thread 2: Thread 2: modified

As we can see, the ThreadLocal variable is not inherited by the child thread, so Thread 2’s value for this variable is null. The InheritableThreadLocal variable is inherited by the child thread, so Thread 2 has the same value for this variable as Thread 1.

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