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

Using C++11 thread_local with other parallel libraries

I have a simple question, can C++11 thread_local be used with other parallel models. For example, can I use it within a function while using OpenMP or Intel TBB to parallel the tasks. Most such parallel programming models hide hardware threads…
Yan Zhou
  • 2,709
  • 2
  • 22
  • 37
9
votes
6 answers

ThreadLocal HashMap vs ConcurrentHashMap for thread-safe unbound caches

I'm creating a memoization cache with the following characteristics: a cache miss will result in computing and storing an entry this computation is very expensive this computation is idempotent unbounded (entries never removed) since: the inputs…
Maian
  • 528
  • 2
  • 5
  • 11
9
votes
2 answers

C++11: Nontrivial Thread Local Static Variable?

I have a class X: class X { ... } I want to do this: void f() { thread_local static X x = ...; ... } (actually I'm using gcc so keyword is "__thread") but I can't because you can only have trivial thread_locals. What is the best…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
8
votes
2 answers

ThreadLocal<> and memory leak

.Net 4. ThreadLocal<> implements IDisposable. But it seems that calling Dispose() doesn't actually release references to thread local objects being held. This code reproduces the problem: using System; using System.Collections.Generic; using…
SergeyS
  • 3,909
  • 2
  • 15
  • 17
8
votes
1 answer

How do the thread local variables in the Rust standard library work?

How do the thread local variables in the Rust standard library work? I looked at the code, but got lost in indirection. It seems that there are different configurations for thread local storage, an OS dependent mode and a fast mode. Which one is the…
Rüdiger Klaehn
  • 12,445
  • 3
  • 41
  • 57
8
votes
1 answer

Bug in clang thread_local initialization

The following code should be creating the in-class thread_local only once, but it ends up initializing it on every access #include #include using std::cout; using std::endl; template class Something { public: …
Curious
  • 20,870
  • 8
  • 61
  • 146
8
votes
3 answers

InheritableThreadLocal value not inherited by ExecutorService threads

import java.util.concurrent.Executors import scala.concurrent.{ExecutionContext, Future} object TestInheritableThreadLocal { def main(args: Array[String]): Unit = { implicit val ec =…
nfsquake
  • 191
  • 2
  • 8
8
votes
3 answers

Questions about using ThreadLocal in a Spring singleton scoped service

In my singleton scoped service class below, all methods in the class require some user context that is known when Service.doA() is called. Instead of passing around info across methods, I was thinking about storing those values in TheadLocal. I have…
Glide
  • 20,235
  • 26
  • 86
  • 135
8
votes
3 answers

Is there no way to iterate over or copy all the values of a Java ThreadLocal?

Context: static ThreadLocal threadLocalMyType = ... What i'd like is to say something like: for (ThreadLocalEntry e: threadLocalMyType.getMapLikeThing() { // Thread t = e.getKey(); // I don't need the thread value right now, but…
Jonas N
  • 1,757
  • 2
  • 21
  • 41
8
votes
0 answers

SecurityContextHolder gives wrong User details

In my Application, We are capturing User details of each transaction from SecurityContextHolder Authentication object. But it gives wrong UserID it seems. Below are the code snippet for your reference. SecurityContext.xml spring-security-3.2…
8
votes
1 answer

Why do some webservers complain about memory leaks they create?

The title might be a bit strong, but let me explain how I understand what happens. I guess this happened with Tomcat (and the message cited comes from Tomcat), but I'm not sure anymore. TL;DR At the bottom there's a summary why I'm claiming that it…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
8
votes
2 answers

ThreadLocal Singleton

I'm running a RESTful java backend on GlassFish. Attached to that is an HTML5 / JS frontend which I can put in a webapp project (and then include the backend as a dependency), or run on an IIS webserver on a different location. CORS is not an issue.…
Pieter-Jan
  • 1,675
  • 2
  • 19
  • 25
8
votes
3 answers

Is it OK to have a thread-local variable with the same name as a non-thread-local variable?

I have a thread local variable envptr and variable that is not thread-local also called envptr. The latter variable is only used in a single thread whose running code does not see the thread-local variable declaration. The thread-local variable is…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
8
votes
4 answers

Is ThreadLocal preferable to HttpServletRequest.setAttribute("key", "value")?

The servlet spec (see my previous question) guarantees that the same thread will execute all Filters and the associated Servlet. Given this, I do not see any use for passing data using HttpServletRequest.setAttribute if there is the option to use a…
necromancer
  • 23,916
  • 22
  • 68
  • 115
7
votes
4 answers

Threads in Spring

I have a Web application using spring and hibernate and struts (it runs on Tomcat) The call sequence is something like this... Struts action calls spring service bean which in turn calls Spring DAO bean. The DAO implementation is a Hibernate…
RN.
  • 997
  • 4
  • 14
  • 31