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.