Listing 1 shows the ThreadLocal interface.
清单1显示了ThreadLocal接口。
Using ThreadLocal to simplify debug logging.
用ThreadLocal简化调试日志纪录。
Listing 2 shows one way to implement ThreadLocal.
清单2显示了ThreadLocal的一个实现方式。
To create a thread-local variable, you instantiate an object of class ThreadLocal.
要创建一个线程局部变量,请实例化类ThreadLocal的一个对象。
For a discussion on the use of ThreadLocal, see Exploiting ThreadLocal to enhance scalability.
有关ThreadLocal的用法的讨论,请参见Exploiting ThreadLocaltoenhancescalability。
Using ThreadLocal makes sense when you need to store variable instances on a per-thread basis.
当您需要以线程为单位存储变量实例时,使用ThreadLocal 很有意义。
By using ThreadLocal, we can reduce contention by giving each thread its own copy of certain critical objects.
通过使用ThreadLocal,给予每个线程它自己的特定临界对象的副本,我们就可以减少争用。
For example, I could have used a singleton with lower-level mechanisms like a ThreadLocal holding a stack of statistics and context.
例如,我可以用带有更底层机制的单体(例如ThreadLocal)持有一堆统计值和上下文。
You can use ThreadLocal variables to store any sort of per-request context information using the per-thread-singleton technique described earlier.
您可以通过前面讲述的每线程单子技术用ThreadLocal变量来存储各种每请求(per - request)上下文信息。
The next-fastest data structure is HashMap, followed by ThreadLocal (essentially, a specialized hash table in which the current thread is the key).
更慢一点儿的数据结构是HashMap,然后是ThreadLocal(这基本上是一个以当前线程作为键的特殊散列表)。
Instead, the thread class was modified to support ThreadLocal by adding an instance variable to thread that holds a HashMap mapping thread-local variables to their values for the current thread.
相反地,人们通过给Thread添加一个实例变量(该变量用于保存当前线程的从线程局部变量到它的值的映射的HashMap)来修改thread类以支持ThreadLocal。
Using ThreadLocal allows us to bypass the complexity of determining when to synchronize in order to achieve thread-safety, and it improves scalability because it doesn't require any synchronization.
使用ThreadLocal使我们可以绕过为实现线程安全而对何时需要同步进行判断的复杂过程,而且因为它不需要任何同步,所以也改善了可伸缩性。
Using ThreadLocal allows us to bypass the complexity of determining when to synchronize in order to achieve thread-safety, and it improves scalability because it doesn't require any synchronization.
使用ThreadLocal使我们可以绕过为实现线程安全而对何时需要同步进行判断的复杂过程,而且因为它不需要任何同步,所以也改善了可伸缩性。
应用推荐