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

Multithreaded use of Regex

Given the following from MSDN: Regex objects can be created on any thread and shared between threads. I have found that for performance, it is better NOT to share a Regex instance between threads when using the ThreadLocal class. Please could…
SergeyS
  • 3,909
  • 2
  • 15
  • 17
7
votes
2 answers

Is it possible to create a ThreadLocal for the carrier thread of a Java virtual thread?

JEP-425: Virtual Threads states that a new virtual thread "should be created for every application task" and makes twice a reference to the possibility of having "millions" of virtual threads running in the JVM. The same JEP implies that each…
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
7
votes
3 answers

Threadlocal counter in Clojure

I have a web app where i want to be able to track the number of times a given function is called in a request (i.e. thread). I know that it is possible to do in a non-thread local way with a ref, but how would I go about doing it thread locally?
Casper
  • 368
  • 4
  • 10
7
votes
3 answers

Thread local behaviour in spring boot

As we know Tomcat has approx 200 threads and Jetty has some default count threads in their respective thread pools. So if we set something in a ThreadLocal per request, will it be there in the thread for life time or will Tomcat clear the…
deepak PATRA
  • 91
  • 1
  • 1
  • 7
7
votes
1 answer

Memory concerns and ThreadLocal

I noticed that ThreadLocal implements IDisposable, implying I should dispose of a thread-local variable when I'm done using it. I'm just curious what the specific concerns are and what I should be careful to do and/or avoid doing. Will a thread's…
devios1
  • 36,899
  • 45
  • 162
  • 260
7
votes
3 answers

When is ThreadLocal preferred over Local Variables?

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…
user3329002
  • 156
  • 2
  • 9
7
votes
2 answers

How/where is the working directory of a program stored?

When a program accesses files, uses system(), etc., how and where is the current working directory of that program physically known/stored? Since logically the working directory of a program is similar to a global variable, it should ideally be…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
7
votes
2 answers

New additional fields in java.lang.Thread, what is the idea?

In Java 8, java.lang.Thread class got 3 new fields: /** The current seed for a ThreadLocalRandom */ @sun.misc.Contended("tlr") long threadLocalRandomSeed; /** Probe hash value; nonzero if threadLocalRandomSeed initialized…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
7
votes
1 answer

thread_local member variable construction

I'm facing some strange behavior with thread_local and not sure whether I'm doing something wrong or it's a GCC bug. I have the following minimal repro scenario: #include using namespace std; struct bar { struct foo { foo ()…
7
votes
1 answer

Java - ThreadLocal vs ConcurrentHashMap

I have a very simple question regarding a performance difference between ThreadLocal and ConcurrentHashMap. In some places in my code I need to maintain a mapping from a Thread to some Object, which has to be thread safe. One option is to use…
Bober02
  • 15,034
  • 31
  • 92
  • 178
7
votes
3 answers

ThreadLocal vs Parameter Passing - What to choose at Interface Design?

I have already read the interesting discussion on following SO thread about ThreadLocal and its use. When and how should I use a ThreadLocal variable? Purpose of ThreadLocal? How does ThreadLocal usage reduce reusability Is it OK to use…
7
votes
3 answers

What are best practices for using thread local storage in .NET?

I have a requirement in my application that I think can be met by using thread local storage, but I'm wondering if it's one of those things that's best to avoid. I have read a few articles on the…
Eric Z Beard
  • 37,669
  • 27
  • 100
  • 145
7
votes
4 answers

What is the Use and need of thread local

I was exploring the Thread local in Java. I could not understand as why do we need this class. I can achieve the same motive if I just simply pass a new object to each thread for execution as same thing happens if i use initialValue(). I simply…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
7
votes
3 answers

Advice on using ThreadLocals to wrap mutable singleton objects

From Java Concurrency in practice Chapter 3.3.3. ThreadLocal Thread-local variables are often used to prevent sharing in designs based on mutable Singletons or global variables. If we wrap the mutable Singleton guy in a ThreadLocal each thread…
Geek
  • 26,489
  • 43
  • 149
  • 227
7
votes
2 answers

How does ThreadLocal usage reduce reusability

The well acclaimed book JCIP says this about ThreadLocal usage : It is easy to abuse ThreadLocal by treating its thread confinement property as a license to use global variables or as a means of creating "hidden" method arguments. Thread-local…
Inquisitive
  • 7,476
  • 14
  • 50
  • 61