thread-local

Using ThreadLocal in instance variables

好久不见. 提交于 2019-12-04 13:11:50
问题 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

Threadlocal memory leak in Threadpool

≯℡__Kan透↙ 提交于 2019-12-04 10:33:55
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 was stopped. Threads are going to be renewed ove r time to try and avoid a probable memory leak. What

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

送分小仙女□ 提交于 2019-12-04 07:56:41
问题 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

How to properly implement RabbitMQ RPC from Java servlet web container?

﹥>﹥吖頭↗ 提交于 2019-12-04 07:21:02
I'd like for incoming Java servlet web requests to invoke RabbitMQ using the RPC approach as described here . However, I'm not sure how to properly reuse callback queues between requests, as per the RabbitMQ tutorial linked above creating a new callback queue per every request is inefficient (RabbitMQ may not cope even if using the Queue TTL feature ). There would generally be only 1-2 RPC calls per every servlet request, but obviously a lot of servlet requests per second. I don't think I can share the callback queues between threads, so I'd want at least one per each web worker thread. My

What are best practices for using thread local storage in .NET?

喜夏-厌秋 提交于 2019-12-04 04:06:21
I have a requirement in my application that I think can be met by using thread local storage, but I'm wondering if it's one of those things that's best to avoid. I have read a few articles on the subject: http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=58 http://msdn.microsoft.com/en-us/library/system.threadstaticattribute(vs.80).aspx I know how to use it, I'm just wondering if I should use it. Any advice, gotchas to be aware of? [Edit] Here's the use case: I funnel all data access through a few methods that do a lot of logging about each query. One thing I log is a complete

How to initialize thread local variable in c++? [duplicate]

北城以北 提交于 2019-12-04 04:00:26
Possible Duplicate: C++11 thread_local in gcc - alternatives Is there any way to fully emulate thread_local using GCC's __thread? I wanted to use the c++11 thread_local to create and use thread_local variable but as it is not yet supported by gcc, I am using gcc specific __thread . The way I declared the variable is myClass { public: static __thread int64_t m_minInt; }; __thread int64_t myClass::m_minInt = 100; When I compile it, I get an error like error: ‘myClass::minInt’ is thread-local and so cannot be dynamically initialized How to properly do it? PS: gcc version: 4.6.3 You need to use

Is it dangerous to use ThreadLocal with ExecutorService?

假装没事ソ 提交于 2019-12-04 03:15:17
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 = userRepository.getUserNameForUserId(userId); userContext.set(new Context(userName)); System.out.println("thread

Is SLF4J thread-safe?

戏子无情 提交于 2019-12-03 23:19:36
问题 I might have a Dog class that has a single instance shared across multiple threads. I plan on using SLF4J for all logging: public class Dog { private Logger logger = LoggerFactory.getLogger(Dog.class); // ...etc. } Is my logger instance thread safe? Why/why not? 回答1: Certainly, everyone assumes that a Logger is going to be thread-safe. But you would need to look at the code / javadocs of the implementation classes behind the facade to be absolutely sure. For specific implementations: Log4j 1

Track dead WebDriver instances during parallel task

你说的曾经没有我的故事 提交于 2019-12-03 20:58:47
I am seeing some dead-instance weirdness running parallelized nested-loop web stress tests using Selenium WebDriver, simple example being, say, hit 300 unique pages with 100 impressions each. I'm "successfully" getting 4 - 8 WebDriver instances going using a ThreadLocal<FirefoxWebDriver> to isolate them per task thread, and MaxDegreeOfParallelism on a ParallelOptions instance to limit the threads. I'm partitioning and parallelizing the outer loop only (the collection of pages), and checking .IsValueCreated on the ThreadLocal<> container inside the beginning of each partition's "long running

Can you use thread local variables inside a class or structure

◇◆丶佛笑我妖孽 提交于 2019-12-03 19:19:13
问题 Like this. struct some_struct { // Other fields ..... __thread int tl; } I'm trying to do that but the compiler is giving me this error. ./cv.h:16:2: error: '__thread' is only allowed on variable declarations __thread int tl; 回答1: Thread-local storage applies to static variables only. There is no point in making non-static structure or class members thread-local. Local (automatic) variables are always specific to the thread that executes the code, but global and static variables are shared