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
0
votes
2 answers

Is ActionContext in Struts 2 unique to the current request?

I'm using a custom interceptor which creates a new db connection, and sets this connection onto the current action before executing the action. After that, the interceptor closes the connection. I'm looking for a convenient way to share this db…
Ali
  • 261,656
  • 265
  • 575
  • 769
0
votes
1 answer

Keeping "current user" in threadlocal

I have a spring-mvc application that currently has two channels - web application and a REST service. Both have user's http session and I can easily get the "current user" in my service classes. Now I need to develop another REST service where there…
user1703531
  • 81
  • 3
  • 7
0
votes
2 answers

Connection Refused when wrapping JMS Connection in ThreadLocal

Running into an issue where connection.start() fails due to java.net.ConnectException: Connection refused: connect when I wrap my javax.jms.TopicConnection in a ThreadLocal, as follows: private ThreadLocal
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
0
votes
2 answers

lookup tables in C++ 11 with multithreading

I have 2 similar situations in a multithreaded C++11 software : an array that I'm using as a lookup table inside a method declaration an array that I'm using as a lookup table declared outside a method and that is being used by different and…
user2485710
  • 9,451
  • 13
  • 58
  • 102
0
votes
1 answer

ThreadLocal cannot be set after redeploying the EJB on JBOSS

I am using JBOSS7. I store some information about the SOAP request in a ThreadLocal variable in order to adding it to the log4j header. My ThreadLocal class: class MyStorage private static final ThreadLocal storage = new…
Viktor
  • 1,325
  • 2
  • 19
  • 41
0
votes
0 answers

EntityManager and ThreadLocal to create a transactional wrapper

In my Java SE/Java EE application I have a DAO interface to wrap around entity manager to allow simple CRUD operations. I also have an extended DAO interface that allows the simple CRUD operations to be performed as a batch operation, meaning all…
user320587
  • 1,347
  • 7
  • 29
  • 57
0
votes
2 answers

Can child's ThreadLocal be modified with the parent's ThreadLocal value?

I am working with InheritableThreadLocal and I know that when creating the children the ThreadLocal will have the default value the same as the parent thread value. But how can I maintain this when the parent's ThreadLocal value is modified? Is…
dianap
  • 27
  • 1
  • 8
0
votes
2 answers

ThreadLocal Vector cleanup in Java

I'm aware of this similar question, but mine concerns a specific case of ThreadLocal cleanup. To maintain state on a thread basis I'm storing an (unknown) number of ThreadLocals in a static Vector. Sometimes a "greedy" thread will need more…
Raul Bertone
  • 146
  • 7
0
votes
1 answer

Do we really need to set the Transaction in ThreadLocal?

I pasted my code below. In our application they set the transaction in thread local. Actually my doubt is why do we need this? What could happen if we didn't set the tranaction in threadlocal? public void beginTransaction() { final String…
0
votes
1 answer

thread local storage versus hashtable

I have a singleton class and I need to store some fields that are specific to each thread. I am thinking about either adding those fields as ThreadLocal variables in the singleton or using a synchronized Hashtable with keys being the thread IDs. Any…
JRR
  • 6,014
  • 6
  • 39
  • 59
0
votes
3 answers

__thread specifier doesn't work in C++ class

I am trying to use the __thread specifier to create a thread local variable. This works OK in the following code: #include #include static __thread int val; int main() { val = 10; } But if I try to use the __thread…
Really Studly
  • 21
  • 1
  • 5
0
votes
2 answers

Race condition: One thread creates static object, another thread uses it before it is finished initializing. How to handle?

I have several places in my code where a function static object is created once, and then used (copied) any time that function is called. One of these functions can be called from any thread. The function doesn't access any shared state other than…
Raptormeat
  • 312
  • 2
  • 14
0
votes
1 answer

Local request context in GWT

In Java, there is ThreadLocal, which can be used to carry some data from one object to another without explicit passing as method argument. I need to intercept GWT request and extract custom HTTP header from it, then I need to store the header value…
jdevelop
  • 12,176
  • 10
  • 56
  • 112
0
votes
1 answer

NHibernate Session per thread with ThreadLocal

Does it make sense to store a NHibernate session within ThreadLocal, is it safe to assume that will be Thread Safe and would solve the lazy loading issues with sessions? Also is it safe to assume that Session will be available for as long as the…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
-1
votes
1 answer

Converting New Java code to an older Java syntax

I'm trying to convert a class written in Java 1.8 to Java 1.4. This is an older Java application but I've never coded in Java 1.4 before. Here is the Java 1.8 code: public class RequestContext implements Filter { private static final…
1 2 3
38
39