Questions tagged [thread-local]

Thread-local is a class from the Java API and the documentation defines it: "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)."

This is a class from the Java API and the documentation defines it this way:

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)."

What questions should have this tag?

  1. Questions that deal with the concept of thread-local directly.
  2. Questions dealing with design-patterns based on using this class.
  3. Questions which have issues while using this class.

Useful Links

  1. Official Documentation
  2. A short tutorial on Thread Local
  3. How to shoot yourself in foot with ThreadLocals Introduction to java ThreadLocal storage

Important questions

  1. When and how should I use a ThreadLocal variable?
  2. Performance of ThreadLocal variable
580 questions
973
votes
26 answers

When and how should I use a ThreadLocal variable?

When should I use a ThreadLocal variable? How is it used?
183
votes
3 answers

What does the thread_local mean in C++11?

I am confused with the description of thread_local in C++11. My understanding is, each thread has unique copy of local variables in a function. The global/static variables can be accessed by all the threads (possibly synchronized access using…
polapts
  • 5,493
  • 10
  • 37
  • 49
130
votes
6 answers

What is "thread local storage" in Python, and why do I need it?

In Python specifically, how do variables get shared between threads? Although I have used threading.Thread before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or…
Mike
  • 3,663
  • 3
  • 20
  • 12
107
votes
3 answers

ThreadStatic v.s. ThreadLocal: is generic better than attribute?

[ThreadStatic] is defined using attribute while ThreadLocal uses generic. Why different design solutions were chosen? What are the advantages and disadvantages of using generic over attributes in this case?
user2341923
  • 4,537
  • 6
  • 30
  • 44
96
votes
6 answers

Performance of ThreadLocal variable

How much is read from ThreadLocal variable slower than from regular field? More concretely is simple object creation faster or slower than access to ThreadLocal variable? I assume that it is fast enough so that having ThreadLocal
Sarmun
  • 2,378
  • 2
  • 22
  • 24
86
votes
5 answers

How is Java's ThreadLocal implemented under the hood?

How is ThreadLocal implemented? Is it implemented in Java (using some concurrent map from ThreadID to object), or does it use some JVM hook to do it more efficiently?
ripper234
  • 222,824
  • 274
  • 634
  • 905
74
votes
8 answers

How to clean up ThreadLocals

Does any one have an example how to do this? Are they handled by the garbage collector? I'm using Tomcat 6.
Ricardo
  • 1,391
  • 4
  • 15
  • 24
63
votes
7 answers

ThreadLocal & Memory Leak

It is mentioned at multiple posts: improper use of ThreadLocal causes Memory Leak. I am struggling to understand how Memory Leak would happen using ThreadLocal. The only scenario I have figured out it as below: A web-server maintains a pool of…
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
60
votes
4 answers

Why is using thread locals in Django bad?

I am using thread locals to store the current user and request objects. This way I can have easy access to the request from anywhere in the programme (e.g. dynamic forms) without having to pass them around. To implement the thread locals storage in…
hekevintran
  • 22,822
  • 32
  • 111
  • 180
53
votes
6 answers

Is it really my job to clean up ThreadLocal resources when classes have been exposed to a thread pool?

My use of ThreadLocal In my Java classes, I sometimes make use of a ThreadLocal mainly as a means of avoiding unnecessary object creation: @net.jcip.annotations.ThreadSafe public class DateSensitiveThing { private final Date then; public…
David Bullock
  • 6,112
  • 3
  • 33
  • 43
48
votes
3 answers

When we should use scala.util.DynamicVariable?

When I read the source of scalatra, I found there are some code like: protected val _response = new DynamicVariable[HttpServletResponse](null) protected val _request = new DynamicVariable[HttpServletRequest](null) There is an interesting class…
Freewind
  • 193,756
  • 157
  • 432
  • 708
40
votes
6 answers

Propagating ThreadLocal to a new Thread fetched from a ExecutorService

I'm running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here) (the thread "spawning" takes place in a AOP Aspect). Now, the main thread is a Resteasy request. Resteasy uses one ore more…
Luciano Fiandesio
  • 10,037
  • 10
  • 48
  • 56
38
votes
6 answers

Purpose of ThreadLocal?

The purpose of ThreadLocal as given here states that the variable is local to any Thread accessing an object containing the ThreadLocal variable. What difference does it make, in having a ThreadLocal variable as a member of a class and then making…
Ajay
  • 7,378
  • 18
  • 57
  • 75
32
votes
4 answers

How to initialize ThreadLocal objects in Java

I'm having an issue where I'm creating a ThreadLocal and initializing it with new ThreadLocal . The problem is, I really conceptually just want a persistent list that lasts the life of the thread, but I don't know if there's a way to initialize…
B T
  • 57,525
  • 34
  • 189
  • 207
31
votes
3 answers

Spring Request scope vs java thread-local

In high volume (~50,000 requests per second) java web-app I'm using ThreadLocal to execute a task which should be executed per request scope. I could achieve the same effect using Spring request scope and I wondered which is better performance…
forhas
  • 11,551
  • 21
  • 77
  • 111
1
2 3
38 39