7

ThreadLocal in Java says that:

The ThreadLocal class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal variable, then the two threads cannot see each other's ThreadLocal variables.

My question is: When we need to get a variable specific to a thread, can't we just declare that variable as a local variable inside a method? Because every thread has its own stack and thus it gets its own copy of variables. Am I missing something here?

Till Helge
  • 9,253
  • 2
  • 40
  • 56
user3329002
  • 156
  • 2
  • 9
  • 1
    Possible duplicate of [When and how should I use a ThreadLocal variable?](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable) – Magnus Jul 19 '16 at 01:45

3 Answers3

6

ThreadLocal is is not an alternative to local variables. You use ThreadLocal for data that have to be static, but which must not be shared between threads.

static final ThreadLocal<MyFoo> myFoo =
    ThreadLocal.withInitial(() -> new MyFoo());

If you have a ThreadLocal variable that is not static, then you're either doing something that's overly complicated, or you're doing something that's just plain wrong.

On the other hand, if you have any variable that is static (whether it is ThreadLocal or not), then you should be aware that that's a design choice that will limit your ability to test and grow the program.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
1

ThreadLocal was meant for different purpose as per oracle documentation.

Have a look at intent of this class:

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).

Below code block generates unique identifiers local to each thread. A thread's id is assigned the first time it invokes ThreadId.get() and remains unchanged on subsequent calls.

 import java.util.concurrent.atomic.AtomicInteger;

 public class ThreadId {
     // Atomic integer containing the next thread ID to be assigned
     private static final AtomicInteger nextId = new AtomicInteger(0);

     // Thread local variable containing each thread's ID
     private static final ThreadLocal<Integer> threadId =
         new ThreadLocal<Integer>() {
             @Override protected Integer initialValue() {
                 return nextId.getAndIncrement();
         }
     };

     // Returns the current thread's unique ID, assigning it if necessary
     public static int get() {
         return threadId.get();
     }
 }

Coming back to your query:

When we need to get a variable specific to a thread, can't we just declare that variable as a local variable inside a method? Because every thread has its own stack and thus it gets its own copy of variables. Am I missing something here?

Yes. You are missing something here.

The scope of variable , which was declared inside a method ends with the method life cycle.

In case of ThreadLocal varaibles, each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible. You can re-enter the thread multiple times in it's life cycle and still you can retain the variable.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

ThreadLocal could be best choice in scenarios when state needs to be associated with thread e.g. for global variables (if semantic permits) because ThreadLocal keeps values of variables confined to a thread; so when a thread T runs get on it, thread T gets the value which was set by itself not by any other threads.

from this article.

Local variable can be utilized when variable is inside thread's class itself and scope is local to each thread. Contrary to this, when variable is outside local scope and exists as part of shared code and semantic permits to keep copy of this variable per thread and not single copy for all threads then ThreadLocal is utilized.

Neha Vari
  • 492
  • 3
  • 14