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的一个实现方式。
Needless to say, the performance of ThreadLocal was quite poor.
不用说,ThreadLocal的性能是相当差的。
To create a thread-local variable, you instantiate an object of class ThreadLocal.
要创建一个线程局部变量,请实例化类ThreadLocal的一个对象。
In this way, we can think of a ThreadLocal as allowing us to create a per-thread-singleton.
这样,我们可以认为ThreadLocal允许我们创建每线程单子。
With these new improvements, ThreadLocal should be faster than other techniques such as pooling.
有了这些提高,ThreadLocal应该比其它技术,如用池,更快。
Using ThreadLocal makes sense when you need to store variable instances on a per-thread basis.
当您需要以线程为单位存储变量实例时,使用ThreadLocal 很有意义。
For a discussion on the use of ThreadLocal, see Exploiting ThreadLocal to enhance scalability.
有关ThreadLocal的用法的讨论,请参见Exploiting ThreadLocaltoenhancescalability。
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)持有一堆统计值和上下文。
One way of getting around this problem is to create a custom SubjectHolder class that wraps a static ThreadLocal to store the current Subject.
一种解决这个问题的方法是创建一个自定义subjectholder类,它包装了一个staticThreadLocal以存储当前Subject。
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(这基本上是一个以当前线程作为键的特殊散列表)。
By using a ThreadLocal in our Singleton, as shown in Listing 3, we can allow any class in our program to easily acquire a reference to a per-thread Connection.
如清单3所示,通过使用“单子”中的ThreadLocal,我们就能让我们的程序中的任何类容易地获取每线程Connection的一个引用。
In JDK 1.2, ThreadLocal was implemented in a manner very similar to Listing 2, except that a synchronized WeakHashMap was used to store the values instead of a HashMap.
在JD k 1.2中,ThreadLocal的实现方式与清单2中的方式非常相似,除了用同步weakhashmap代替hashmap来存储values之外。
Other applications for ThreadLocal in which pooling would not be a useful alternative include storing or accumulating per-thread context information for later retrieval.
其它适合使用ThreadLocal但用池却不能成为很好的替代技术的应用程序包括存储或累积每线程上下文信息以备稍后检索之用这样的应用程序。
ThreadLocal variables are different from normal variables in that each thread has its own individually initialized instance of the variable, which it accesses via get() or set() methods.
ThreadLocal变量与常规变量的不同之处在于,每个线程都有其各自初始化的变量实例,这通过get()或set()方法予以评估。
It isn't a particularly good implementation (although it is quite similar to the initial implementation), as it would likely perform poorly, but it illustrates clearly how ThreadLocal behaves.
它不是一个特别好的实现(虽然它与最初实现非常相似),所以很可能性能不佳,但它清楚地说明了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使我们可以绕过为实现线程安全而对何时需要同步进行判断的复杂过程,而且因为它不需要任何同步,所以也改善了可伸缩性。
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。
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。
应用推荐