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
6
votes
1 answer

Java 8 to Java 17 ThreadLocal issue

I have code that works well in Java 8, but it doesn't work when I migrate it to Java 17. It involves ThreadLocal and CompletableFuture.runAsync. Here are the classes: public class UriParameterHandler { } public class DateRangeEntity { public…
6
votes
3 answers

Is there any benefit in puting a ThreadSafe object on a ThreadLocal?

I recently saw a piece of code which used a ThreadLocal object and kept a ConcurrentHashMap within it. Is there any logic/benefit in this, or is it redundant?
RonK
  • 9,472
  • 8
  • 51
  • 87
6
votes
2 answers

How to create a thread local variable inside of a Rust struct?

I need a thread local variable, ideally stored in a struct which currently stores most of my program's global state. The first way I can see to do this is to use the thread_local! macro, however I would like to keep this thread local within my…
PiRocks
  • 1,708
  • 2
  • 18
  • 29
6
votes
2 answers

Effect of ThreadLocals and side-by-side classloading

Assuming class A{ private static final ThreadLocal tl = new ThreadLocal(); } If A is loaded in just one classloader on the vm, the value of t1 is obvious. But what happens to t1 if A is loaded side-by-side in two different…
krosenvold
  • 75,535
  • 32
  • 152
  • 208
6
votes
1 answer

How to set ThreadLocal for parallelStream

I have a thread and it contains ThreadLocal variable. I need to use parallelStream() inside the above mentioned thread. Need to call myService which uses the thread local variable. Is there any mechanism to set the ThreadLocal when using…
Akila
  • 187
  • 2
  • 9
6
votes
3 answers

volatile vs threadLocal in java

Lets take SimpleDateFormat as an example since it is not thread safe. I could allow each thread to have its own copy of SimpleDateFormat using threadLocal like this: private static final ThreadLocal formatter = new…
j2emanue
  • 60,549
  • 65
  • 286
  • 456
6
votes
5 answers

threadlocal variables in a servlet

Are the threadlocals variables global to all the requests made to the servlet that owns the variables? I am using resin for the server. Thanks for awnser. I think I can make my self more clear. The specific Case: I want to: initialize a static…
user2427
  • 7,842
  • 19
  • 61
  • 71
6
votes
1 answer

Segmentation fault when accessing statically initialized __thread variable

Consider the following code: #include __thread bool foo = true; int main() { printf("foo = %d\n", foo); return 0; } Compile and run with: $ g++ tls.cpp -o tls -o tls $ ./tls On some systems -- such as Amazon Linux 2013.09.0,…
Hongli
  • 18,682
  • 15
  • 79
  • 107
6
votes
0 answers

How to properly implement RabbitMQ RPC from Java servlet web container?

I'd like for incoming Java servlet web requests to invoke RabbitMQ using the RPC approach as described here. However, I'm not sure how to properly reuse callback queues between requests, as per the RabbitMQ tutorial linked above creating a new…
John M
  • 1,469
  • 17
  • 41
6
votes
1 answer

Can other threads modify thread-local memory?

Let's say I have the following declaration: thread_local std::atomic local_var; Are modifications of local_var from other threads allowed? That is, if I communicate the address of the local_var to another thread, won't modifying local_var…
user283145
6
votes
3 answers

Multiple objects in a ThreadLocal

Can we set more than one object in a ThreadLocal ?
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
6
votes
3 answers

Confused about ThreadLocal

I just learned about ThreadLocal this morning. I read that it should always be final and static like: private static final ThreadLocal threadLocal = new ThreadLocal(); (Session is a Hibernate Session) My confusion is this: …
badgerduke
  • 1,013
  • 5
  • 16
  • 28
5
votes
3 answers

Using ThreadLocal in instance variables

Do Java ThreadLocal variables produce thread-local values if they are used as instance variables (e.g., in a method that generates thread-local objects), or must they always be static to do so? As an example, assume a typical scenario where…
PNS
  • 19,295
  • 32
  • 96
  • 143
5
votes
5 answers

ThreadLocal performance vs using parameters

I am currently implementing a runtime (i.e. a collection of functions) for a formulas language. Some formulas need a context to be passed to them and I created a class called EvaluationContext which contains all properties I need access to at…
boggy
  • 3,674
  • 3
  • 33
  • 56
5
votes
1 answer

Thread local data in linux kernel module

Is it possible to create thread local data in a linux kernel module? I need to store some data for each process/thread calling my module. Is there an easy way of using thread local data, or do I have to resort to writing a hash map which uses the…
ar31
  • 3,576
  • 1
  • 25
  • 22