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

What is the function of multithreaded ThreadLocal

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the role of multi-threaded ThreadLocal". In daily operation, I believe many people have doubts about what the role of multi-threaded ThreadLocal is. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what is the role of multi-threaded ThreadLocal"! Next, please follow the small series to learn together!

What is ThreadLocal?

ThreadLocal is for shared variables. If multiple threads may use this variable, and this variable is expected to be managed by the thread itself, the change does not need to be known by other threads, that is, the change result can have an effect on itself. You can specify the variable as ThreadLocal.

To put it bluntly: ThreadLocal means that each thread clones a variable from the main memory area and then uses it independently.

package test.thread.threadlocal;public class ThreadLocalTest { 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 ThreadLocalTest test = new ThreadLocalTest(); test.set(); System.out.println(test.getLong()); System.out.println(test.getString()); Thread thread1 = new Thread(){ public void run() { 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()); }}

Print Results:

1main10Thread-01main

From the code execution results, thread1 changes to longLocal and stringLocal have no effect on the value of the main thread.

At this point, the study of "what is the role of multithreaded ThreadLocal" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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