thread-local

Will the ThreadLocal object be cleared after thread returned to Thread Pool?

让人想犯罪 __ 提交于 2019-12-03 16:32:39
问题 Will the contents that are stored in the ThreadLocal storage during an execution be cleared automatically when the thread is returned to ThreadPool (as would be expected) ?? In my application I am putting some data in ThreadLocal during some execution but if next time the same Thread is being used, then I am finding the obsolete data in ThreadLocal storage. 回答1: The ThreadLocal and ThreadPool don't interact with one another unless you do this. What you can do is a a single ThreadLocal which

Why did the language designers of Java preferred chaining over open addressing for most hash based structures except for some like ThreadLocal? [closed]

人盡茶涼 提交于 2019-12-03 14:43:25
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I know the difference between Open Addressing and Chaining for resolving hash collisions . Most of the basic hash based data structures like HashSet , HashMap in Java primarily use chaining technique. I read that

Is ThreadLocal safe to use with Tomcat NIO Connector

泄露秘密 提交于 2019-12-03 13:26:23
This just came to mind when testing the Tomcat NIO connector during my load tests. I make use of ThreadLocal's additionally I use Spring, which I know in several places it also makes use of it. Since the NIO connector does not have a thread per connection, I worry that it may result in very hard to find bugs if a ThreadLocal object was shared with another thread before it had been cleaned up. However, I assume that this is not an issue as it is not a documented warning that I could find, nor have I found any other posts warning about this. I assume that the NIO connector has no effect to the

How to force a Java thread to close a thread-local database connection

本小妞迷上赌 提交于 2019-12-03 10:53:58
When Using a thread-local database connection, closure of the connection is required when the thread exists. This I can only do if I can override the run() method of the calling thread. Even that is not a great solution, since at time of exit, I don't know if a connection has ever been opened by that thread. The problem is in fact more general: How to force a thread to call some finalisation method of a thread-local object when it exits. I looked at the sources of java 1.5 and found that the thread local map is set to null, which will eventually cause the garbage collection to call finalize()

ThreadLocal<> and memory leak

左心房为你撑大大i 提交于 2019-12-03 09:47:26
问题 .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 System.Collections.Concurrent; using System.Linq; using System.Threading; namespace ConsoleApplication2 { class Program { class ThreadLocalData { // Allocate object in LOH public int[] data = new int[10 * 1024 * 1024]; }; static void Main(string[] args) {

Using ThreadLocal in instance variables

匆匆过客 提交于 2019-12-03 08:23:23
Do Java ThreadLocal variables produce thread-local values if they are used as instance variables (e.g., in a method that generates thread-local objects), or must they always be static to do so? As an example, assume a typical scenario where several, expensive to initialize objects of a class that is not thread-safe, need to be instantiated in a single static initialization block, stored in static variables of a single class (e.g., in a Map data structure) and from then on used for intensive processing by numerous different threads. To achieve thread safety, obviously a different copy of each

ThreadLocal<> and memory leak

心已入冬 提交于 2019-12-03 00:20:19
.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 System.Collections.Concurrent; using System.Linq; using System.Threading; namespace ConsoleApplication2 { class Program { class ThreadLocalData { // Allocate object in LOH public int[] data = new int[10 * 1024 * 1024]; }; static void Main(string[] args) { // Stores references to all thread local object that have been created var threadLocalInstances = new

How to identify and remove Threads/ThreadLocals initiated from our webapp in Java?

孤人 提交于 2019-12-02 23:40:54
Whenever I stop or redeploy the webapp, I see lot of errors similar to, msg=The web application [] created a ThreadLocal with key of type [] (value []) and a value of type [] (value []) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid probable memory leak I'm not creating any ThreadLocals in my app but referencing many libraries which may be creating these ThreadLocals. We are currently using Tomcat 7. I've already gone through other similar questions [Memory leak when redeploying application in Tomcat or What are these

Is it really my job to clean up ThreadLocal resources when classes have been exposed to a thread pool?

Deadly 提交于 2019-12-02 17:52:50
My use of ThreadLocal In my Java classes, I sometimes make use of a ThreadLocal mainly as a means of avoiding unnecessary object creation: @net.jcip.annotations.ThreadSafe public class DateSensitiveThing { private final Date then; public DateSensitiveThing(Date then) { this.then = then; } private static final ThreadLocal<Calendar> threadCal = new ThreadLocal<Calendar>() { @Override protected Calendar initialValue() { return new GregorianCalendar(); } }; public Date doCalc(int n) { Calendar c = threadCal.get(); c.setTime(this.then): // use n to mutate c return c.getTime(); } } I do this for the

ThreadLocal - using as context information for REST API with spring-boot

柔情痞子 提交于 2019-12-02 12:08:57
I have some spring-boot application (it exposes rest api). The mentioned REST API is secured by spring-security . Everything is fine, however now I need to set context for servicing request. Setting context is about choosing datasource in depends on user context. The key is that RoutingDataSource need to use this context. (This context must be set directly after authenticating request due to other causes, I have also other thread which use RoutingDataSource, but no invoked by request (no user context)). These things I can do, however my doubts are concerned on thread-safety of context and