threadlocal
In languages such as IBM Xml,fortran, thread-local variables are provided at the syntactic level, but Java provides no such support at the syntactic level, but provides java.lang.ThreadLocal at the beginning of JDK 1.2 (and supports generics at JDK1.5 start) threadloca
Instead of a thread, Theadlocal saves the container of the thread localization object, and when an object running in a multit
Http://www.cnblogs.com/xrq730/p/4854813.htmlThreadlocal is actually quite simple, because there are three public methods in the class: set (T value), get (), remove (). The first analysis of the source clearly know threadlocal is what to use, reuse, the final summary, explain threadlocal take such ideas. Three theoretical foundationsIn the analysis of Threadlocal
space".Threadlocal is not used to solve variable thread-sharing security, but rather provides a way to save objects.(iv) Source code analysisView source code with THREADLOCAL.THREADLOCALMAP variable in each threadBind variables and thread through map in threadlocalpublic void Set (T value) { Thread T = Thread.CurrentThread (); Get Threadlocalmap bind the thread and object variable threadlocalmap map = getmap (t); if (map!
Threadlocal Source Code AnalysisThreadlocal is actually quite simple, because there are three public methods in the class: set (T value), get (), remove (). The first analysis of the source clearly know threadlocal is what to use, reuse, the final summary, explain threadlocal take such ideas. Three theoretical foundationsIn the analysis of
Java is required for work, and there are many things to learn. I have to say goodbye to. Net temporarily.
When I was looking at spring, I suddenly thought of threadstaticattribute in. Net (allowing static variables to be different in different threads ).In many cases, the content in each execution thread is fixed, such as the servlet currentuser/httpcontext/session in the Web environment. in many cases, these variables are transferred to the back-
The following is the threadlocal test code. For more information, see note
Package COM. jadyer. thread. local; import Java. util. random; /*** threadlocal test * @ see ================================== ========================================================== ================================================================= *@ the role and purpose of see
virtual-machine shutdown hook.For example, you can write a thread that sends a message, and when the virtual machine hangs, it calls the incoming thread, sending a message.There should also be a mechanism in the thread that can execute a previously registered event before a thread is hung up, or a listener is listening on the state of the thread, thus making callbacksWhen you get a notification that the thread hangs, you can remove all the variables related to the thread from the delete
: Threadlocal.set () in the object will exist in the current thread in a map, and the current ThreadLocal itself as a key, can only say that the map can guarantee the unique, the map of the object if it is stored through new, The object is unique to the thread if it is not persisted by another thread reference, otherwise there may be a multithreaded concurrency problem because it is a reference typeTest code:
public static ThreadLocalString>
Threadlocal is not a thread, but a local variable of thread, and perhaps naming it as threadlocalvariable is easier to understand. Therefore, the code to write thread-local variables in Java is relatively clumsy, resulting in thread-local variables not being well-popularized in Java developers. Threadlocal interface me
Java provides a type of threadlocal that has member variables of that type, and each thread that uses that variable retains a copy of the property's backup data, and the operation of that property inside the thread is the data that is backed up by itself. Therefore, member variables declared as threadlocal types are thread-safe.A simple test of the difference bet
references, the system cleans up objects of this type of reference before the memory is going to overflow. For example, an external data source: A file, a database, a network, etc. can be declared as a reference to that type.WeakReference, weak references, objects that are associated with weak references that are recognized by the JVM as weak can be recycled at the next GC.The difference between SoftReference and WeakReference is that GC time is postponed as much as possible.Phantom, virtual Re
backgroundIn Java program implementation, we often apply to the mechanism of transaction, transaction opening in the business layer, creating the database connection, invoking the DAO layer method for database access, the process needs to pass the database connection connection as the parameter to the DAO layer method. Obviously, such an implementation is not conducive to the reuse of the DAO layer method, when we do not use transactions, we need to c
In the above, "Java Code Quality Improvement: Selection of Synchronization Objects", we present a scenario: the train station has 3 ticket windows and 100 seats for a train. We completed the above function by locking a reliable synchronization object.Now, let's turn, each window is responsible for a ride. For example, a window on the 1th train ticket, second window on the sale of the Ticket No. 2nd. But they need to start selling tickets at the same t
Threadlocal's official API is interpreted as:"This class provides thread-local (thread-local) variables. 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. ThreadLocal instances are typically private static fields in a class that want to associate the state with a thread (for example
Java-18.6 synchronized synchronization and ThreadLocal on other objects from the ground up to eliminate the synchronization problem of shared objects
This section describes synchronization and ThreadLocal on other objects.
In the previous chapter, we used
1. synchronized synchronization on other objects
class ThreadA implements Runnable {private Object object = n
The threadlocal differs from the thread member variable, threadlocal This class provides a thread-local variable. This local variable is not the same as the general member variable, threadlocal variables are used by multiple threads, each thread can only get a copy of the variable, which is described in the Java API, t
I. Test code
Public class xy_threaddata{Private Static Integer Data = 0;Private Static Map Private Static threadlocal
Public static void setdata (integer value){Data = value;}
Public static integer getdata (){System. Out. println ("threadname:" + thread. currentthread () + "data value:" + data );Return data;}
Public static void setmapdata (integer value){Map. Put (thread. currentthread (), value );}
Public static integer getmapdata (){Object OBJ = ma
The Threadlocal class is designed to solve multi-threaded security issues. Thread safety means that each thread operates its own variables and does not affect the values of other threads.In many cases, threadlocal is easier and more convenient than using the synchronized synchronization mechanism to solve thread safety problems, and results programs have higher concurrency.1
Thread safety has always been the program of the focus of the Apes, multithreading has been more annoying topic, presumably we have encountered various problems, I will no longer be tired. Of course, there are many solutions, this blog provides a good way to solve the problem of thread safety.
First of all, we have a simple understanding of threadlocal, followed by the example + analysis, the last sentence summed up.
1. Meet Threalocal
Knowing
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.