Summary of Threadlocal

Source: Internet
Author: User

For Threadlocal's understanding, this post is written in a very good position: http://www.iteye.com/topic/103804

Personal Summary:

existence Meaning: Threadlocal is used to resolve data isolation between multiple threads.

Focus on understanding:

1) First of all, ThreadLocal is not used to solve multi-threaded access problems of shared objects, in general, through the Threadlocal.set () to the object in the thread is the thread itself, the other threads are not required to access, but also inaccessible. Different objects are accessed in each thread.

2) In addition, it is said that threadlocal enables each thread to maintain its own independent object, not by Threadlocal.set (), but by creating an object through the operation of the new object in each thread, creating a copy or a copy of each thread that is not an object. The reference to this newly created object is saved to a map of each thread by Threadlocal.set (), each thread has a map that executes Threadlocal.get (), and each thread pulls the object from its own map and So the objects in their own thread are taken out, and the threadlocal instance is used as the key to the map.

Note: if Threadlocal.set () goes in the same object that multiple threads share, then the Threadlocal.get () of multiple threads gets the shared object itself, or there is a concurrent access problem.


in short, Threadlocal is not used to solve the problem of object sharing access, but mainly provides methods of preserving objects and convenient way of accessing objects to avoid parameter passing. Summed up two points:

1) Each thread has its own Threadlocalmap class object, which holds the thread's own object into it, and the threads can access their objects correctly.
2) Use a common threadlocal static instance as key, save a reference to a different object to the threadlocalmap of a different thread, and then execute the threads everywhere through the get () of this static threadlocal instance Method gets the object that the thread holds for itself, avoiding the hassle of passing the object as a parameter.

For threadlocal applications, I think the best thing to do is to access the objects by multiple instances of threads (one instance per thread), and this object is used in many places.

Case:

Initialization

<span style= "FONT-SIZE:18PX;" >private static threadlocal<boolean> ThreadLocal = new threadlocal<boolean> ();</span>

Based on the business requirements, set the instance object to the appropriate value:

<span style= "FONT-SIZE:18PX;" >                                         if (list1 = = NULL | | list1.size () = = 0) {Basedao.execute (Update_status_fasle, New object[]{ Getsystemdate.gettime (), ServerIP}); Basedao.execute (Update_status_true, New Object[]{getsystemdate.gettime (), ServerIP}); Threadlocal.set (true);} Else{list List2 = basedao.queryforlist (Query_data); if (list2! = null && list2.size () ==1) {Basedao.execute ( Update_status_fasle, New Object[]{getsystemdate.gettime (), ServerIP}); Basedao.execute (Update_status_true, new Object[]{getsystemdate.gettime (), ServerIP}); Threadlocal.set (true);} Else{threadlocal.set (false);} </span>


as a control to get the value of the instance "this place is judged"
<span style= "FONT-SIZE:18PX;" >             if (threadlocal.get () | | startflag) {logger.info ("start callback, etc.);</span>


finally: see what methods the API provides



For remove (), this is the description:





Summary of Threadlocal

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.