thread-local

New additional fields in java.lang.Thread, what is the idea?

岁酱吖の 提交于 2019-12-10 03:09:10
问题 In Java 8, java.lang.Thread class got 3 new fields: /** The current seed for a ThreadLocalRandom */ @sun.misc.Contended("tlr") long threadLocalRandomSeed; /** Probe hash value; nonzero if threadLocalRandomSeed initialized */ @sun.misc.Contended("tlr") int threadLocalRandomProbe; /** Secondary seed isolated from public ThreadLocalRandom sequence */ @sun.misc.Contended("tlr") int threadLocalRandomSecondarySeed; as it said in Javadoc for being exclusively managed by class java.util.concurrent

Is it dangerous to use ThreadLocal with ExecutorService?

♀尐吖头ヾ 提交于 2019-12-09 16:28:42
问题 I was going through the concept of ThreadLocals on the below blog : https://www.baeldung.com/java-threadlocal It says that "Do not use ThreadLocal with ExecutorService" It illustrates below example for using ThreadLocals. public class ThreadLocalWithUserContext implements Runnable { private static ThreadLocal<Context> userContext = new ThreadLocal<>(); private Integer userId; private UserRepository userRepository = new UserRepository(); @Override public void run() { String userName =

ThreadLocal (and Singleton) in EJB Container

不打扰是莪最后的温柔 提交于 2019-12-07 17:35:31
问题 I've written an authorization system which relies on objects representing the current user. To simplify programming and increase performance I want to hold those objects in a ThreadLocal after the user has logged in. It looks like this: public class UserCache { private static final ThreadLocal<User> cache = new ThreadLocal<User>(); public User getCurrentUser() { return cache.get(); } public void setCurrentUser(User user) { cache.set(user); } } I've read that static elements make clustering

Inheritable thread local in .NET

人走茶凉 提交于 2019-12-07 15:24:20
问题 .NET 4.0 introduced ThreadLocal<T> class, which is great. Now, my app use case requires something like Java's InheritableThreadLocal class. Is there anything like that in .NET? If not, how can a new thread initializes its thread locals variables with those from the parent thread? I suppose I need to make use the initialization factory method but not sure what to put in there. 回答1: The closest thing I can think of is using ILogicalThreadAffinitive and storing your object in the thread's

Thread locals in Python - negatives, with regards to scalability?

浪子不回头ぞ 提交于 2019-12-07 14:56:16
问题 I'm wondering if there are some serious implications I might be creating for myself by using thread locals. I noticed that in the case of Flask, they use thread locals, and mention that it can cause issues with servers that aren't built with threads in mind. Is this an outdated concern? I'm using thread locals with Django for a few things, deploying with NGINX in front of UWSGI, or Gunicorn, on Ubuntu 10.04 with Postgres (not that the OS or DB probably matter, but just for clarity). Do I need

Minimizing SecureRandom performance problems in multithreaded environment?

*爱你&永不变心* 提交于 2019-12-07 07:16:24
问题 (This is with Java 8 and Tomcat 8 on SLES, FWIW.) How worried do I need to be about performance problems with SecureRandom (specifically the SHA1PRNG algorithm of the SUN provider) after the initial seeding when I am using a single SecureRandom instance in multiple threads? SecureRandom is thread-safe so that implies some level of potential contention? I don't see anything in Java 8 Javadocs that discusses this for SecureRandom though I see that the Javadocs for Random do specifically warn

Is it OK to use ThreadLocal for storing the requested Locale?

故事扮演 提交于 2019-12-06 21:14:46
问题 I am working on internationalizing user entered data in a rather large Client/Server (HTTP (Hessian) is used for communication) application which is stored in a database. Users can choose the language they want to see and there is a default language which is used when a translation in the requested language is not present. Currently a data class may look like this: class MyDataClass { private Long id; private String someText; /* getters and setters */ } After internationalization it could

Is thread-local storage persisted between backgroundworker invocations?

纵然是瞬间 提交于 2019-12-06 19:54:53
问题 Are backgroundworker threads re-used? Specifically, if I set a named data slot (thread-local storage) during the DoWork() method of a backgroundworker, will the value of that data slot persist, potentially to be found be some other thread at a later time? I wouldn't have thought so, but I have this bug... EDIT: This blog post suggests that BackGroundWorker utilises a ThreadPool, which implies that Threads are re-used. So the question becomes; do re-used threads potentially persist thread

Why does ThreadLocal utility always return null in Spring MVC app?

廉价感情. 提交于 2019-12-06 09:48:57
问题 I wrote this utility class to save temporary data in a Spring MVC app: public abstract class FooUtil { private static final ThreadLocal<String> threadFoo = new ThreadLocal<String>(); public static String getFooId(){ return threadFoo.get(); } public static void setFooId(String fooId){ threadFoo.set(fooId); } public static void removeFooId(){ threadFoo.remove(); } } So I call FooUtil.setFooId("foo") . But when I later call FooUtil.getFooId() , it always returns null . Do I need a constructor?

Threadlocal memory leak in Threadpool

对着背影说爱祢 提交于 2019-12-06 04:32:34
问题 I am getting threadlocal memory leak errors in Tomcat and I am using ThreadPool, but have no implementation of ThreadLocal in my webapp. SEVERE: The web application [/myWebApp] created a ThreadLocal with key of type [org.a pache.http.impl.cookie.DateUtils$DateFormatHolder$1] (value [org.apache.http.imp l.cookie.DateUtils$DateFormatHolder$1@4c2849]) and a value of type [java.lang.re f.SoftReference] (value [java.lang.ref.SoftReference@1e67280]) but failed to rem ove it when the web application