thread-local

Extending java's ThreadLocal to allow the values to be reset across all threads

对着背影说爱祢 提交于 2019-12-02 11:21:16
问题 After looking at this question, I think I want to wrap ThreadLocal to add a reset behavior. I want to have something similar to a ThreadLocal, with a method I can call from any thread to set all the values back to the same value. So far I have this: public class ThreadLocalFlag { private ThreadLocal<Boolean> flag; private List<Boolean> allValues = new ArrayList<Boolean>(); public ThreadLocalFlag() { flag = new ThreadLocal<Boolean>() { @Override protected Boolean initialValue() { Boolean value

use .Net UdpClient in a multithreaded environment

空扰寡人 提交于 2019-12-01 08:52:24
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); } } My questions is: I know that UdpClient isn't thread-safe (according to MSDN documentation), what

Java thread safe database connections

你。 提交于 2019-12-01 08:30:21
I'm writing a servlet that handles each request by accessing and modifying some table(s) in the database. I want the connections to the database to be thread safe. I don't want to use already existing libraries/frameworks for this (spring, hibernate, etc.). I know I can use java's ThreadLocal for this in the following way : public class DatabaseRegistry { //assume it's a singleton private Properties prop = new Properties(); public static final ThreadLocal<Connection> threadConnection = new ThreadLocal<Connection>(); private Connection connect() throws SQLException { try { // This will load the

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

南楼画角 提交于 2019-12-01 06:34:13
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 functions in the hot path. I'm using GCC and Linux. Portability is not required. no. gcc does currently

Java thread safe database connections

跟風遠走 提交于 2019-12-01 05:46:29
问题 I'm writing a servlet that handles each request by accessing and modifying some table(s) in the database. I want the connections to the database to be thread safe. I don't want to use already existing libraries/frameworks for this (spring, hibernate, etc.). I know I can use java's ThreadLocal for this in the following way : public class DatabaseRegistry { //assume it's a singleton private Properties prop = new Properties(); public static final ThreadLocal<Connection> threadConnection = new

Is ThreadLocal preferable to HttpServletRequest.setAttribute(“key”, “value”)?

北城余情 提交于 2019-12-01 01:40:18
问题 The servlet spec (see my previous question) guarantees that the same thread will execute all Filters and the associated Servlet. Given this, I do not see any use for passing data using HttpServletRequest.setAttribute if there is the option to use a ThreadLocal (assuming you clean up properly). I feel that there are two benefits to using ThreadLocal : type-safety and better performance because no string keys or maps are being used (except probably into a thread collection by (non-string)

How does pthread_key_t and the method pthread_key_create work?

偶尔善良 提交于 2019-11-30 14:00:52
I am having some trouble figuring out how pthread_key_t and pthread_key_create work. From my understand, each thread has TLS (thread local storage) and that a key is used to access the thread local storage. What I do not get is when a key is created, does every thread get to use it? Lets say Thread 0 creates key 0, can Thread 1 then use key 0? If Thread 1 used key 0, would it access its own TLS or Thread 0's TLS? Is there some global array or something that keeps track of all the keys being used? pthread_keys are just what you said, thread local storage referred to by a common key. So multiple

ThreadLocal Resource Leak and WeakReference

两盒软妹~` 提交于 2019-11-30 13:44:30
问题 My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern or example for correctly using ThreadLocal with WeakReference, if one exists. For instance, in this code snippet where would the WeakReference be introduced? static class DateTimeFormatter { private static final ThreadLocal<SimpleDateFormat> DATE

What are the advantages of instance-level thread-local storage?

百般思念 提交于 2019-11-30 12:01:52
问题 This question led me to wonder about thread-local storage in high-level development frameworks like Java and .NET. Java has a ThreadLocal<T> class (and perhaps other constructs), while .NET has data slots, and soon a ThreadLocal<T> class of its own. (It also has the ThreadStaticAttribute, but I'm particularly interested in thread-local storage for member data.) Most other modern development environments provide one or more mechanisms for it, either at the language or framework level. What

InheritableThreadLocal value not inherited by ExecutorService threads

孤者浪人 提交于 2019-11-30 09:55:58
import java.util.concurrent.Executors import scala.concurrent.{ExecutionContext, Future} object TestInheritableThreadLocal { def main(args: Array[String]): Unit = { implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(2)) val tl: InheritableThreadLocal[String] = new InheritableThreadLocal[String]() tl.set("InitialValue") Future { println("111 " + Thread.currentThread() + tl.get()) Future { println("222 " + Thread.currentThread() + tl.get()) } } Thread.sleep(3000) Future { tl.set("NewInitialValue") println("333 " + Thread.currentThread() + tl.get()) Future { println("444