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

Is ThreadLocal correct for my use-case?

This is how our functional code is structured. Functional code calls ConfigAdapter which can fetch the configurations from any other store. Now with the change in the requirements and to decouple the configurations, we created a new class called…
0
votes
2 answers

How to store per thread contextual data in c#?

My concrete need is to store remote IP so it can be printed while logging, without explicitly passing it down every method call. A sort of per thread environment variable scope. I thought of having a per thread Singleton so logger can access it. Is…
Igor Gatis
  • 4,648
  • 10
  • 43
  • 66
0
votes
1 answer

How to a reference of a thread, and verification on ThreadLocal

I'm looking for verification on the following use of ThreadLocal. I have a service, say ServiceA running on a set of processes, say processSetX in the system. Which processSetX will be on ServiceA isn't known until runtime and may vary. The…
0
votes
3 answers

Connection Pooling vs Per-Thread JDBC Connection

Which of these approaches is better: connection pooling or per-thread JDBC connections?
java_geek
  • 17,585
  • 30
  • 91
  • 113
0
votes
1 answer

Use thread local store lua_State

I need to use Lua in IOCP, and use Thread local Storage to store lua_State *. I should use lua_close() destroy the lua_State before the thread destroyed, but the work thread is created by IOCP. The question is when is the right time to call…
bywayboy
  • 3
  • 4
0
votes
1 answer

ThreadLocal leak in webservice client

I've a stand alone application which makes use of VMware's Java Webservice API, this is a wrapper around the webservice client. I'm using a fixed thread pool of size 5 to invoke the APIs in library. After running for a while (a day or two), the…
0
votes
1 answer

How to release heap memory of thread local storage

I have a structure used for thread local storage like this: namespace { typedef boost::unordered_map< std::string, std::vector > YYY; boost::thread_specific_ptr cache; void initCache() { //The first time called by the current…
rahman
  • 4,820
  • 16
  • 52
  • 86
0
votes
1 answer

TestNG: Collaborators local to a test

Suppose I have one test class with three test methods. The tests may either run serially or in two separate threads. Each test method requires its own instance of a collaborator (such as a resource of some kind). However, this would not seem to…
Kelvin Chung
  • 1,327
  • 1
  • 11
  • 23
0
votes
0 answers

Thread Local storage getting corrupted after Access Violation Exception

I have some data stored in thread local storage. Basically I have a per thread some kind of attributes stored in TLS. In the TLS we put the custom class object instance CMyAttributes*. This is an reference counted attributes holder As an…
Venki
  • 2,129
  • 6
  • 32
  • 54
0
votes
1 answer

What are the pros and cons of using an in memory DB rather than a ThreadLocal

we have been using ThreadLocal so far to carry some data so as to not clutter the API. However below are some of issues of using thread local that which I dont like 1) over the years the data items being carried in thread local has increased 2)…
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0
votes
3 answers

Inside the Web container how more than 1 object of the same class is getting created/managed which has same reference variable

Probably the stupidest question you have ever heard. Inside the Web container how more than 1 object of the same class is getting created/managed which has same reference variable... Let me explain with an example. Inside my controller class I have…
0
votes
1 answer

ThreadGuard or ThreadLocal for Selenium grid thread concurrency issues?

Which one is better - ThreadGuard or ThreadLocal for Selenium grid thread concurrency issues?
Amrit
  • 433
  • 3
  • 19
0
votes
0 answers

org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction cannot be cast to

I have this class TransactionManager: public class TransactionManager { private static ThreadLocal currentTransaction; public static Session getCurrentSession() { if(currentTransaction == null) { …
bogdan.rusu
  • 901
  • 4
  • 21
  • 41
0
votes
2 answers

capture the third party web service session Id during Spring Security Session

I have implemented Spring security in a Spring MVC web application. For the authentication purpose I am using LDAP and for authorization I am calling a third party Web Service that provides me All the authorizations and also a Session Id. Once user …
0
votes
2 answers

Simulate thread local variables

I want to simulate thread local variables for non static members, something like this : template< typename T, unsigned int tNumThread > class ThreadLocal { private: protected: T mData[tNumThread]; unsigned int _getThreadIndex() { …
MRB
  • 3,752
  • 4
  • 30
  • 44