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
20
votes
4 answers

Will the ThreadLocal object be cleared after thread returned to Thread Pool?

Will the contents that are stored in the ThreadLocal storage during an execution be cleared automatically when the thread is returned to ThreadPool (as would be expected) ?? In my application I am putting some data in ThreadLocal during some…
Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30
19
votes
6 answers

What are the advantages of instance-level thread-local storage?

This question led me to wonder about thread-local storage in high-level development frameworks like Java and .NET. Java has a ThreadLocal class (and perhaps other constructs), while .NET has data slots, and soon a ThreadLocal class of its own.…
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
18
votes
3 answers

How does a C++ compiler implement thread local storage in C++0x?

How does c++ complier implement thread local storage in C++0x I have searched this in google. But I can't find anything about this. Does anyone have any material about this ??
Yuncy
  • 761
  • 1
  • 9
  • 20
17
votes
6 answers

On which platforms is thread local storage limited and how much is available?

I was recently made aware that thread local storage is limited on some platforms. For example, the docs for the C++ library boost::thread read: "Note: There is an implementation specific limit to the number of thread specific storage objects that…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
16
votes
3 answers

Java - How to check value of 'ThreadLocal' variables in Eclipse?

I have couple of ThreadLocals populated in my web app. And, while remote debugging the webapp, I want to see the value of these ThreadLocal variables in Eclipse (just like the way Eclipse shows other variables in the variables tab in Debug…
peakit
  • 28,597
  • 27
  • 63
  • 80
15
votes
1 answer

c++11 thread_local keyword support in visual studio 11

So there's a list of c++11 features supported by visual studio. thread_local support is marked as partial. I was unable to find an explanation of what exactly partial support means here. Did they just alias __declspec(thread)? I could just use…
Eloff
  • 20,828
  • 17
  • 83
  • 112
15
votes
1 answer

Does the specification guarantee that operations on sequential Java streams have to stay in the current thread?

Does the specification guarantee, that all operations on sequential Java Streams are executed in the current thread? (Except for "forEach" and "forEachOrdered") I explicitly ask for the specification, not what the current implementation does. I can…
user194860
  • 626
  • 5
  • 12
15
votes
2 answers

Pre-initializing a pool of worker threads to reuse connection objects (sockets)

I need to build a pool of workers in Java where each worker has its own connected socket; when the worker thread runs, it uses the socket but keeps it open to reuse later. We decided on this approach because the overhead associated with creating,…
raffian
  • 31,267
  • 26
  • 103
  • 174
14
votes
9 answers

How to force a Java thread to close a thread-local database connection

When Using a thread-local database connection, closure of the connection is required when the thread exists. This I can only do if I can override the run() method of the calling thread. Even that is not a great solution, since at time of exit, I…
Joel Shemtov
  • 3,008
  • 2
  • 22
  • 22
14
votes
1 answer

Akka and Java libraries that use ThreadLocals

What has kept me from using Akka regularly (in Java) is a concern I have with libraries that use ThreadLocals. That is I think some Akka Dispatchers may cause ThreadLocal variables to be left behind, or missing all together. So the obvious solution…
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
14
votes
7 answers

ThreadLocal usage in enterprise application

If my web application and ejb application are on the same machine (on same JVM) and all the ejb calls are local calls , will the use of ThreadLocal create any issue while passing information from web to ejb? Any workaround if the ejb calls are…
Snake Eye
  • 535
  • 3
  • 13
13
votes
2 answers

Is thread-local storage persisted between backgroundworker invocations?

Are backgroundworker threads re-used? Specifically, if I set a named data slot (thread-local storage) during the DoWork() method of a backgroundworker, will the value of that data slot persist, potentially to be found be some other thread at a later…
Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
13
votes
4 answers

Is it dangerous to use ThreadLocal with ExecutorService?

I was going through the concept of ThreadLocals on the below blog : https://www.baeldung.com/java-threadlocal It says that "Do not use ThreadLocal with ExecutorService" It illustrates below example for using ThreadLocals. public class…
Hitesh
  • 432
  • 3
  • 13
13
votes
4 answers

Synchronized and local copies of variables

I'm looking at some legacy code which has the following idiom: Map myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization…
Epsilon Prime
  • 4,576
  • 5
  • 31
  • 34
13
votes
4 answers

ThreadLocal vs local variable in Runnable

Which one among ThreadLocal or a local variable in Runnable will be preferred? For performance reasons. I hope using a local variable will give more chances for cpu caching, etc.
sab
  • 9,767
  • 13
  • 44
  • 51
1 2
3
38 39