thread-local

java threadlocal singleton - what is it?

帅比萌擦擦* 提交于 2019-11-28 04:36:16
问题 In layman speak, what does it mean when somebody says an object is a threadlocal singleton in Java? I was at a lecture about Java Server Faces, and everytime the FacesContext was spoken of - the instructor always reminded us that it is a threadlocal singleton. 回答1: There is only one unique instance of the FacesContext per thread. The FacesServlet creates a ThreadLocal<FacesContext> on the beginning of the HTTP servlet request and removes it on the end of the HTTP servlet response associated

Performance of ThreadLocal variable

空扰寡人 提交于 2019-11-28 03:53:01
How much is read from ThreadLocal variable slower than from regular field? More concretely is simple object creation faster or slower than access to ThreadLocal variable? I assume that it is fast enough so that having ThreadLocal<MessageDigest> instance is much faster then creating instance of MessageDigest every time. But does that also apply for byte[10] or byte[1000] for example? Edit: Question is what is really going on when calling ThreadLocal 's get? If that is just a field, like any other, then answer would be "it's always fastest", right? Running unpublished benchmarks, ThreadLocal.get

Threadlocal counter in Clojure

元气小坏坏 提交于 2019-11-28 00:40:06
问题 I have a web app where i want to be able to track the number of times a given function is called in a request (i.e. thread). I know that it is possible to do in a non-thread local way with a ref, but how would I go about doing it thread locally? 回答1: There's a tool for this in useful called thread-local . You can write, for example, (def counter (thread-local (atom 0))) . This will create a global variable which, when deref ed, will yield a fresh atom per thread. So you could read the current

How to initialize ThreadLocal objects in Java

别来无恙 提交于 2019-11-27 22:48:10
I'm having an issue where I'm creating a ThreadLocal and initializing it with new ThreadLocal . The problem is, I really conceptually just want a persistent list that lasts the life of the thread, but I don't know if there's a way to initialize something per-thread in Java. E.g. what I want is something like: ThreadLocal static { myThreadLocalVariable.set(new ArrayList<Whatever>()); } So that it initializes it for every thread. I know I can do this: private static Whatever getMyVariable() { Whatever w = myThreadLocalVariable.get(); if(w == null) { w = new ArrayList<Whatever>();

ThreadLocal to store ServletRequest and Response in servlet: what for?

跟風遠走 提交于 2019-11-27 21:36:15
Once I have came across a pattern, where ServletRequest and response objects are put to servlet's local ThreadLocal variables. The servlet class has also methods to get current request and response objects. So in order to get these objects you still need to operate with servlet object. What is the point of having these ThrealLocal local variables? The point is to have the request and response objects in classes that would otherwise would not have them (for example they are not servlets). One example are JSF managed beans - their methods do not take HttpServletRequest parameters, and so you can

Propagating ThreadLocal to a new Thread fetched from a ExecutorService

给你一囗甜甜゛ 提交于 2019-11-27 17:51:51
I'm running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here ) (the thread "spawning" takes place in a AOP Aspect). Now, the main thread is a Resteasy request. Resteasy uses one ore more ThreadLocal variables to store some context information that I need to retrieve at some point in my Rest method call. Problem is, since the Resteasy thread is running in a new thread, the ThreadLocal variables are lost. What would be the best way to "propagate" whatever ThreadLocal variable is used by Resteasy to the new thread? It seems that Resteasy uses

Purpose of ThreadLocal? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 05:59:50
This question already has an answer here: When and how should I use a ThreadLocal variable? 24 answers The purpose of ThreadLocal as given here states that the variable is local to any Thread accessing an object containing the ThreadLocal variable. What difference does it make, in having a ThreadLocal variable as a member of a class and then making it local to a Thread, rather than having a local variable to the Thread itself? leonm A thread is a unit of execution and so multiple thread can execute the same code at the same time. If multiple threads execute on an object/instance at the same

How to make sure that I am not sharing same socket between two threads at a same time? [duplicate]

天涯浪子 提交于 2019-11-27 03:45:59
问题 This question already has an answer here: Do not share same socket between two threads at the same time 7 answers I have a code in which I am dealing with sockets and I need to make sure that I don't share same socket between two threads . In my below code, I have a background thread which runs every 60 seconds and calls updateLiveSockets() method. In the updateLiveSockets() method, I iterate all the sockets I have and then start pinging them one by one by calling send method of SendToQueue

ThreadStatic v.s. ThreadLocal<T>: is generic better than attribute?

一曲冷凌霜 提交于 2019-11-27 03:36:58
[ThreadStatic] is defined using attribute while ThreadLocal<T> uses generic. Why different design solutions were chosen? What are the advantages and disadvantages of using generic over attributes in this case? Jim Mischel Something the blog post noted in the comments doesn't make explicit, but I find to be very important, is that [ThreadStatic] doesn't automatically initialize things for every thread. For example, say you have this: [ThreadStatic] private static int Foo = 42; The first thread that uses this will see Foo initialized to 42 . But subsequent threads will not. The initializer works

Are C++ exceptions sufficient to implement thread-local storage?

早过忘川 提交于 2019-11-27 01:47:36
问题 I was commenting on an answer that thread-local storage is nice and recalled another informative discussion about exceptions where I supposed The only special thing about the execution environment within the throw block is that the exception object is referenced by rethrow. Putting two and two together, wouldn't executing an entire thread inside a function-catch-block of its main function imbue it with thread-local storage? It seems to work fine, albeit slowly. Is this novel or well