The ThreadLocal data is stored in one thread and does not require lock synchronization. Theoretically ThreadLocal can be
So that a variable has a copy on each thread. ThreadLocal is commonly used to mask private variables of a thread, such as "and
or other resources. Also, it is used to maintain counters, statistics, or ID generation for each thread
M
The Threadlocal class, which represents a thread-local variable, lets each thread create a copy of the variable by placing the data in the threadlocal. It can also be viewed as another way of threading synchronization by creating a thread-local copy of a variable for each thread, thereby avoiding conflicts when concurrent threads read and write to the same variable resource at the same time.
Examples are a
If a lock is a thread-ordered scramble for resources, then threadlocal is to have a resource for each thread.For example, the lock is for 100 people to compete for a pen area to write, threadlocal is 100 people each have a pen, in the turn when they write.Write a simple example: Public classDemoImplementsrunnable{StaticThreadlocalNewThreadlocal(); Static classTest {PrivateInteger in; PublicInteger Getin ()
Share:A and B need to share the same thread, and a group of identical A and B shares also have a set of threads, and the two groups are not affected by each other.Code:Package Cn.itcast.lesson6;import Java.util.random;public class Threadlocaltest {public static void main (string[] args) { for (int i=0;i Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced. Java line: Singleton hidden
Content from, Zhang Xiaoxiang teacher's Zhang Xiaoxiang-java multi-threading and concurrent Library Advanced Application Video tutorialPackage Com.lipeng;public class MyThreadLocalTest2 {private final static mythreadlocalJava Multi-Threading and concurrency Applications-(5)-How to use the Threadlocal class gracefully
What we are learning today is how to achieve the sharing of variable data within the scope of the thread itself, and the various threads are independent from each other, and each one is maintained, that is, what we call the Threadlocal function.First, the conceptYou can store the data that each thread uses with the corresponding thread number in a map collection, and you can implement thread-wide sharing of the same variables by using data from that c
Telephone interview asked Threadlocal what to pay attention to use. Find relevant information on the Internet
Threadlocal can save local variables for the current thread, while inheritablethreadlocal can pass local variables of the parent thread to child threads when creating child threads.
If a thread pool (such as executor) is used, the child thread still exists and is pooled even though the parent
Preface: The work will use threadlocal, first study summarizes a wave. There is no place to welcome comments to point out.
DefinedThreadlocal is not a thread, but a local variable of thread. These variables are different from their ordinary counterparts, because each thread that accesses a variable (through its get or set method) has its own local variable, independent of the initialized copy of the variable.RoleEach thread is implemented with it
ThreadLocal Concept:Threadlocal is not used to solve the problem of object shared access, but mainly provides methods of saving objects and convenient object access to avoid parameter passing.Threadlocal is not a thread, but a local variable of thread, and when using threadlocal to maintain variables threadlocal provides a separate copy of the variable for each t
The Threadlocal class can be understood as threadlocalvariable (thread-local variables), providing access interfaces or methods such as get and set, which hold a separate copy of each thread that uses the variable. So get always returns the most recent value that the current execution thread sets when it calls the set. Threadlocal
To sum up, for multithreading resource sharing problem, synchronization mech
I. Why use threadlocal:In the previous blog post, we controlled the transaction by passing the connection way, which can achieve the goal, but it makes people look uncomfortable,If it involves invoking multiple service, do I still have to pass connection from the controller layer?Threadlocal's usage see the previous blog, which guarantees that instance variables of a class have a separate copy in each thread.So that the instance variables in other threads are not affectedTwo. How to use
The reason why thread-safety problems arise
Thread safety problems are caused by global variables and static variables.
Ii. Thread-safety issues
Simpledateformate SDF = new SimpleDateFormat (); use Sdf.parse (DATESTR); Sdf.format (date); There is a reference to the Caleadar object within the SDF. In the source code sdf.parse (DATESTR); Calendar.clear (); and Calendar.gettime (); Time to get calendar
If thread A calls Sdf.parse () and Calendar.clear () has not executed calendar.gettime (),
set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); } ThreadLocalMap getMap(Thread t) { return t.threadLocals; }
For details, let's look at the source code, but one thing is verifiable, that is, the thread copy created in Threadlocal, which can be cleared without calling remove, the jvm will perform automatic garbage collection when it finds that the thread
Principle of ThreadLocal mode: threadlocal Mode
In earlier versions of JDK, a solution is provided to solve the problem of multi-thread concurrency: java. lang. ThreadLocal class. When maintaining variables, the ThreadLocal class actually uses an independent copy of ThreadLo
This article is mainly organized by Lu Zhou, "Struts2 technical insider"
Design pattern: it is a summary of a set of code Design experience formed after repeated practices by programmers.
ThreadLocal mode: a solution to solve the Data Sharing Problem in multi-threaded programs.
1. thread security issues
In traditional Web development, the most common way to process Http requests is to implement Servlet objects to respond to Http requests. Servl
Analysis of ThreadLocal and threadlocal
This is my first blog. It is not very clear, but I hope it will help you.
First, clarify the role of this class. The ThreadLocal class is used to provide a copy of the variable for each thread, that is, the local variable of each thread. Every thread has the value of this variable in its own stack space (the object is refer
ThreadLocal: simple understanding and threadlocal understandingIn the code of the java open-source project, you can see the ThreadLocal attribute in a class:
private static ThreadLocal
I saw ThreadLocal while reading a book, but s
Directory
Analysis of Threadlcoal Source code
Garbage collection of Threadlocal
Java reference
Recycling of Threadlocal
Recycling of Threadlocalmap in each thread
Memory leak issues
Summarize
Reference
Analysis of Threadlcoal Source code
We know that threadlocal is used to
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.