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

Using Vaadin Hibernates ThreadLocal.get() returns null after first call

I'm working on a web application in the Vaadin framework to retrieve some data from a MySQL database back-end. The application itself is a survey system to keep track of software consultants skills (and rate them). Some colleagues of mine had…
0
votes
0 answers

Creating request scope by passing object among all threads

The system receives a request R. Then we create a unique object "tracker". We want to use tracker associated with R if the current code is processing request R or any of the runnables that help complete it. The access is tough if the system has…
0
votes
1 answer

Remove ThreadLocal object within a Spring MVC website?

I have the following classes: public class AppContext { private static final ThreadLocal contextHolder = new ThreadLocal() { @Override protected AppContextData initialValue() { …
curious1
  • 14,155
  • 37
  • 130
  • 231
0
votes
1 answer

ThreadLocal InitialValue is called twice

I set the ThreadLocal initial value common for all threads. When calling constructor I changed the value and it printed correctly. But when starting the thread, it again changed to initial value? Is that expected? If yes, what is the explanation?…
Anirban B
  • 507
  • 8
  • 27
0
votes
1 answer

What is the best way to set ThreadLocal with Id from request messageHeader on Server side JAX-WS Webservice

I have a JAX-WS Webservice exposed to client. When Client invokes it, messageheader will be populated with UserId. I am trying to set this value in thread local using utility class when request comes from client and remove it before sending response…
user5657519
0
votes
1 answer

Thread local storage constructor g++

As non trivial thread local storage variables have an overhead, I would like to use the __attribute__((constructor)) indication on some function in order to initialize thread local data ( being intended that the function will be called each time a…
George Kourtis
  • 2,381
  • 3
  • 18
  • 28
0
votes
2 answers

Threadlocal reference to hibernate session impl, causing session impl not to get garbage collected

I am using thread local to manage my hibernate sessions. Recently I have been seeing OutOfMemory exceptions on my production server. I ran Eclipse MAT on the heap_dump and saw a lot of my sessions are not getting garbage collected even though they…
Tiffany
  • 1
  • 2
0
votes
1 answer

ThreadLocal get() gives unexpected result when set() method is not invoked

I have base class which contains ThreadLocal : @Singleton public class BaseView extends HttpServlet { protected ThreadLocal locale = new ThreadLocal(); private Locale getLocale() { return (Locale)…
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
0
votes
1 answer

Unable to get Integer Value of Variable Stored in Java Thread Local Object

I am trying to get the integer value of a Thread Local Object as in the code below, but it keeps throwing an error. However, when I try to display it as a string, it displays. My Question: How can I achieve extracting the Integer value? Note:…
Naz_Jnr
  • 570
  • 1
  • 7
  • 17
0
votes
2 answers

In Java, how to close ThreadLocal resource when thread is finished

I have a third party Java library that provides a resource. TheResource resource = TheResource.create() Sometime later, when I'm done with the resource, I'm supposed to close the resource resource.close() According to the library documentation,…
Jake
  • 1,518
  • 2
  • 14
  • 20
0
votes
3 answers

Design pattern name: Is it a factory?

The following class shows something similar to a real use case. It returns always the same instance for the same thread. public class LookingForName { private static final ThreadLocal threadLocal = new…
deamon
  • 89,107
  • 111
  • 320
  • 448
0
votes
1 answer

Threadlocal values iList ordering

Hi i have an example which creates 2 threads. My question is when i output the values it always prints 1000 before 999. Is it possible to print 999 before 1000. Just want to know how are they ordered? static void Main(string[] args) { …
RStyle
  • 875
  • 2
  • 10
  • 29
0
votes
2 answers

resultset.getTimestamp and ThreadLocal warning Java

I have started getting lots of warnings whenever I restart my server lately. The warning looks like this: the web application [/xxx] created a ThreadLocal with key of type [net.sourceforge.jtds.jdbc.Support$1] (value…
Johannes
  • 135
  • 1
  • 2
  • 10
0
votes
1 answer

Using threadlocal to bind instance of a class to a thread

I have some difficulty in understanding the use of ThreadLocal in the below declaration. Does it mean, even though the instance is declared as static each thread will have it's own instance of DomainEventPublisher class private static final…
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
0
votes
0 answers

Inconsistent Data by ThreadLocal<>

I was trying to add dynamic rows to a GridView using below variable. private static ThreadLocal> productList = null; Below is the event handler adding the rows dynamically. protected void Button1_Click(object sender, EventArgs e) { …
Jaish Mathews
  • 766
  • 1
  • 9
  • 25