In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the case analysis of JavaThreadLocal applications. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
A little bit of eye contact
ThreadLocal, which means ThreadLocal Variable (thread local variable), may be more appropriate to name it ThreadLocalVar.
The function of thread local variable (ThreadLocal) is very simple, which is to provide a copy of the variable value for each thread that uses the variable, so that each thread can change its own copy independently without conflicting with other thread's copy. From a thread's point of view, it is as if every thread fully owns the variable.
The use of the ThreadLocal class is very simple, providing only the following three public methods:
T get (): returns the value in the current thread copy in this thread local variable. Void remove (): deletes the value of the current thread in this thread's local variable. Void set (T value): sets the value in the current thread copy in this thread local variable.
Second actual combat
1 code
Class Account {/ * defines a variable of type ThreadLocal that will be a thread local variable and each thread retains a copy of the variable * / private ThreadLocal name = new ThreadLocal ()
/ / define a constructor public Account (String str) {this.name.set (str) that initializes name member variables
/ / the following code accesses the value System.out.println ("- -" + this.name.get ()) of the current thread's copy of name;}
/ / setter and getter methods public String getName () {return name.get ();} public void setName (String str) {this.name.set (str) of name
}} class MyTest extends Thread {
/ / define a member variable of Account type private Account account; public MyTest (Account account, String name) {super (name); this.account = account;} public void run () {
/ / cycle 10 times for (int I = 0; I < 10; iTunes +) {
/ / when I = = 6, the output changes the account name to the current thread name if (I = = 6) {account.setName (getName ());}
/ / output the account name and circular variable System.out.println (account.getName () + "account I value:" + I) of the same account;} public class ThreadLocalTest {public static void main (String [] args) {
/ / start two threads, which share the same Account
/ / there is a name of Account in the main thread, and there is also a name of Account in thread An and thread B. the three do not interfere with Account at = new Account ("original name").
/ * although the two threads share the same account, that is, there is only one account name, but because the account name is ThreadLocal, each thread has its own copy of the account name, so after I = 6, you will see that the two threads will see different account names when they access the same account. * / new MyTest (at, "thread A") .start ()
New MyTest (at, "thread B"). Start ();}}
2 run
-initial name null account I value: 0null account I value: 0null account I value: 1null account I value: 1null account I value: 2null account I value: 2null account I value: 3null account I value: 3null account I value: 4null account I value: 4null account I value: 5 thread An account I value: 6 thread An account I value: 7 thread An account I value: 8 thread An account I value Account I value: 9null account I value: 5 thread B account I value: 6 thread B account I value: 7 thread B account I value: 8 thread B account I value: 9
3 description
There are actually three copies of the account name, one for the main thread and one for each of the two threads started. Their values do not interfere with each other, and each thread has its own ThreadLocal variable, which is the purpose of ThreadLocal.
This is the end of the case analysis on the actual combat of JavaThreadLocal applications. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.