Thread Continue - 4

zhaozj2021-02-16  69

Threadlocal performance Although the thread local variable has already been famous and included in many thread frames, including the POSIX PTHReads specification, the initial Java thread design is omitted, but it is only added to the Java platform version 1.2. In many ways, Threadlocal is still in development; it is rewritten in version 1.3, and the version 1.4 has been rewritten once, and it is specifically for performance issues twice.

In JDK 1.2, Threadlocal implementation is very similar to the manner in Listing 2, except for the synchronous weakhashmap instead of HashMap to store VALUES. (At the expense of some extra performance overhead, using WeakhashMap solves the problem that the THREAD objects that cannot be garbage-collecting the Thread object cannot be said.) Don't say, Threadlocal's performance is quite worse.

The Threadlocal version provided by the Java platform version 1.3 has been as better as possible; it does not use any synchronization, so there is no scalability problem, and it does not use weak references. Conversely, people have modified the Threadlocal by adding an instance variable to thread (this variable is used to save the "HashMap from the thread local variable to its mapping) to support Threadlocal. Because the process of retrieving or setting a thread local variable does not involve reading and writing of data that may be read or written by another thread, you can implement threadlocal.get () and set () without any synchronization. Moreover, since each thread value is stored in the own Thread object, it is also possible to garbage collection when garbage recovery is used for Thread.

Unfortunately, even if these improvements, the performance of Threadlocal in Java 1.3 is still surprisingly slow. According to my rough measurement, THREADLOCAL.GET () operations are performed in the Sun 1.3 JDK on the Dual processor Linux system, and the time consuming is about twice the same. The reason why the performance is the thread.currentthread () method is very large, accounting for two-thirds of the threadlocal.get () run time. Although there are these shortcomings, JDK 1.3 threadlocal.get () is still much more faster than the contention, so if there is any serious disputeless place (maybe there are very many threads, or synchronized blocks are frequently performed, or synchronized The block is large), Threadlocal may still be much efficient.

In the latest version of the Java platform, that is, version 1.4b2, Threadlocal and Thread.currentthread () have greatly improved. With these improvements, Threadlocal should be more faster than other techniques, such as pool. Since it is simpler than other technologies, it is more difficult to mistake, and people will eventually find an effective way to avoid undesirable interactions between threads.

Threadlocal's benefits ThreadLocal can bring a lot of benefits. It is often used to depict state classes to thread, or encapsulate non-threaded security classes to securely use the easiest way to safely use in multithreaded environments. Using Threadlocal allows us to bypass when to implement thread security, it needs to be synchronized, and scalability is also improved because it does not need any synchronization. In addition to simplicity, use Threadlocal storage per-thread or each thread context information in archiving in terms of archiving - by using ThreadLocal, the object stored in Threadlocal is unclear, thus simplifying Judging whether a class is working safely. I hope that you have been having fun from this series, and I have learned knowledge. I also encourage you to go to my discussion forum to study multithreaded problems.

About the author Brian Goetz is a software consultant, has been professional software developers in the past 15 years. He is the Chief Consultant of Quiotix, which is engaged in software development and consultation, located in Los Altos, California. Please check the list of papers that Brian has published and will be published in popular industry publications. Can contact Brian via brian@quiotix.com.

转载请注明原文地址:https://www.9cbs.com/read-16411.html

New Post(0)