Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Thread closure in Java

2025-02-24 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report