thread-local

Synchronized and local copies of variables

£可爱£侵袭症+ 提交于 2019-12-21 07:15:03
问题 I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization on local variable 'myMap' Is this the appropriate synchronization and why? Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (someGlobalInstance.getMap()) { item = myMap.get(myKey); } 回答1: The reason this is flagged as a problem is

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

99封情书 提交于 2019-12-21 03:39:34
问题 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

use .Net UdpClient in a multithreaded environment

こ雲淡風輕ζ 提交于 2019-12-19 10:19:10
问题 I have an instance of a class (lets call it A) which serves some threads, this instance only sends UDP packets via the UdpClient class. It initialize the the UdpClient in its constructor and only serves to send the packets. It looks something like: public class A{ private UdpClient m_Client; public class A(string host, int port){ m_Client = new UdpClient(host, port); } public void Send(string dataToSend){ var data= encoding.GetBytes(dataToSend); client.BeginSend(data, data.Length, null, null)

Is there any way to fully emulate thread_local using GCC's __thread?

混江龙づ霸主 提交于 2019-12-19 08:29:28
问题 The C++11 standard contains a new addition - the thread_local specifier - which makes static variables thread-local. The standard thread_local supports non-trivial types - those with constructors and destructors. GCC unfortunately supports only trivial types via __thread specifier provided as extension. Is there's a way to emulate thread_local on top of __thread ? The implementation of __thread is very fast (equivalent to regular variable plus two indirections), so I want to avoid library

What is so bad with threadlocals

99封情书 提交于 2019-12-18 14:07:05
问题 Everybody in Django world seems to hate threadlocals(http://code.djangoproject.com/ticket/4280, http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser). I read Armin's essay on this(http://lucumr.pocoo.org/2006/7/10/why-i-cant-stand-threadlocal-and-others), but most of it hinges on threadlocals is bad because it is inelegant. I have a scenario where theadlocals will make things significantly easier. (I have a app where people will have subdomains, so all the models need to have access

ThreadLocal usage in enterprise application

安稳与你 提交于 2019-12-18 12:21:19
问题 If my web application and ejb application are on the same machine (on same JVM) and all the ejb calls are local calls , will the use of ThreadLocal create any issue while passing information from web to ejb? Any workaround if the ejb calls are remote? Will ThreadLocal information be available from web application to ejb application? Is use of ThreadLocal advisable in such scenario? 回答1: For the first question, there is no problem as long as you remove the ThreadLocal variables at the end of

When we should use scala.util.DynamicVariable?

半世苍凉 提交于 2019-12-18 10:12:24
问题 When I read the source of scalatra, I found there are some code like: protected val _response = new DynamicVariable[HttpServletResponse](null) protected val _request = new DynamicVariable[HttpServletRequest](null) There is an interesting class named DynamicVariable . I've looked at the doc of this class, but I don't know when and why we should use it? It has a withValue() which is usually be used. If we don't use it, then what code we should use instead, to solve the problem it solved? (I'm

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

安稳与你 提交于 2019-12-18 06:51:45
问题 Context: static ThreadLocal<MyType> 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 it might be useful for // something else. MyType theMyType = e.getValue(); // [...do something with theMyType...] } 回答1: One way would be to handle this manually: use a wrapper of ThreadLocal (extend it) whenever a value is set, keep a ( static ) Map of

threadlocal variables in a servlet

喜欢而已 提交于 2019-12-18 03:00:43
问题 Are the threadlocals variables global to all the requests made to the servlet that owns the variables? I am using resin for the server. Thanks for awnser. I think I can make my self more clear. The specific Case: I want to: initialize a static variable when the request starts the execution. be able to query the value of the variable in the further executions of methods called from the servlet in a thread safety way until the request ends the execution 回答1: Short answer: Yes. A bit longer one:

Performance of ThreadLocal variable

此生再无相见时 提交于 2019-12-17 17:32:21
问题 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