In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to achieve thread closure in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
What is thread closure?
When accessing shared variables, locks are often needed to ensure data synchronization. One way to avoid synchronization is not to share data. If you only access data in a single thread, you don't need synchronization. This technique is called thread closure. In the Java language, some class libraries and mechanisms are provided to maintain thread closure, such as local variables and ThreadLocal classes. This article focuses on how to use ThreadLocal classes to ensure thread closure.
Understand the ThreadLocal class
The ThreadLocal class associates a value in a thread with the object that holds the value. It provides get and set methods that keep a separate copy for each thread that uses the variable, so get is always the latest set value of the current thread set.
First of all, let's look at an example, which comes from http://www.cnblogs.com/dolphin0520/p/3920407.html.
Public class Test1 {ThreadLocal longLocal = new ThreadLocal (); ThreadLocal stringLocal = new ThreadLocal (); public void set () {longLocal.set (Thread.currentThread (). GetId ()); stringLocal.set (Thread.currentThread (). GetName ());} public long getLong () {return longLocal.get ();} public String getString () {return stringLocal.get () } public static void main (String [] args) throws InterruptedException {final Test1 test = new Test1 (); test.set (); System.out.println (test.getLong ()); System.out.println (test.getString ()); Thread thread1 = new Thread (()-> {test.set (); System.out.println (test.getLong () System.out.println (test.getString ();}); thread1.start (); thread1.join (); System.out.println (test.getLong ()); System.out.println (test.getString ());}}
Run the program, and the result of the code output is:
one
Main
ten
Thread-0
one
Main
You can see from this code that both the mian thread and the thread1 thread do keep their own copies, and their copies do not interfere with each other.
ThreadLocal source code parsing
To parse the ThreadLocal class from a source point of view, this class is stored in the java.lang package, and this class has many methods.
There is another ThreadLocalMap class inside it, which mainly has set (), get (), setInitialValue and other methods.
First, take a look at the set method to get the map of the current Thread. If it does not exist, create a new one and set the value. If there is a set value, the source code is as follows:
Public void set (T value) {Thread t = Thread.currentThread (); ThreadLocalMap map = getMap (t); if (map! = null) map.set (this, value); else createMap (t, value);}
Tracking createMap, you can see that it creates a ThreadLocalMap based on Thread.
Void createMap (Thread t, T firstValue) {t.threadLocals = new ThreadLocalMap (this, firstValue);}
T.threadLocals is a variable of the current thread, that is, the data of ThreadLocal is stored in the threadLocals variable of the current thread, so it can be seen that the data stored with ThreadLocal is thread-safe. Because it is for different threads, using the set method of ThreadLocal will determine whether the thread has its threadLocals member variable according to the thread, if not, build one, and save the data if so.
ThreadLocal.ThreadLocalMap threadLocals = null
ThreadLocalMap is an internal class of ThreadLocal. The source code is as follows:
Static class ThreadLocalMap {static class Entry extends WeakReference
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.