6.ThreadLocal Class and application skills

Source: Internet
Author: User

1 ImportJava.util.Random;2 3 /**4 * Threadlocal class and application skills5  * *******************************************************************6 * Each thread invokes the set method of the global Threadlocal object, which is equivalent to adding a record to its internal map, key7 * respectively is the respective thread, values is the value that the respective set method wears in. Threadlocal.clear () can be called at the end of the thread;8 * method, which frees up memory faster and does not call, because the associated threadlocal variable can also be freed automatically after the thread ends. 9 * Implement the encapsulation of the threadlocal variable, so that the outside world does not directly manipulate the threadlocal variableTen * This application is relatively rare in the encapsulation of basic types of data.  One * Encapsulation of data of an object type, which is more common, is to have a class create a separate instance object for different threads, respectively.  A * Summary: - * A threadlocal represents a variable, so it can only put one data, you have two variables to be within the thread range - * Shared, you define two Threadlocal objects. What if there are 100 variables to be shared by the thread? Package The threadlocal the * into one singleton class.  -  *  - * The thought in Struts2 is threadlocal mode -  *  +  * ******************************************************************* - * Register hook notification when virtual machine hangs + * Runtime->addshutdownhook (Thread hook) A  * @authorlitaiqing at  */ -  Public classThreadlocaltest { -     Private StaticThreadlocal<integer> ThreadLocal =NewThreadlocal<integer>(); -     //private static threadlocal<mythreadscopedata> Mythreadscopedata = new threadlocal<mythreadscopedata> (); -      Public Static voidMain (string[] args) { -          for(inti = 0; I < 2; i++){ in             NewThread (NewRunnable () { - @Override to                  Public voidrun () { +                     intdata =NewRandom (). Nextint (); -System.out.println (Thread.CurrentThread (). GetName () + "Get put data:" +data); the threadlocal.set (data); *                     //Mythreadscopedata.set (New Mythreadscopedata ("name" +data,data)); $Mythreadscopedata.getthreadinstance (). SetName ("name" +data);Panax Notoginseng mythreadscopedata.getthreadinstance (). setage (data); -                     NewA (). get (); the                     NewB (). get (); +                 } A }). Start (); the         } +     } -     Static classa{ $          Public voidget () { $             intdata =threadlocal.get (); -System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Get put data:" +data); -             //Mythreadscopedata myData = Mythreadscopedata.get (); theMythreadscopedata MyData =mythreadscopedata.getthreadinstance (); -System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Getmydata:" + mydata.getname () + "," +mydata.getage ());Wuyi         } the     } -     Static classb{ Wu          Public voidget () { -             intdata =threadlocal.get (); AboutSystem.out.println ("B from" + Thread.CurrentThread (). GetName () + "Get put data:" +data); $             //Mythreadscopedata myData = Mythreadscopedata.get (); -Mythreadscopedata MyData =mythreadscopedata.getthreadinstance (); -System.out.println ("B from" + Thread.CurrentThread (). GetName () + "Getmydata:" + mydata.getname () + "," +mydata.getage ()); -         } A     } + } the classmythreadscopedata{ -     Private Staticthreadlocal<mythreadscopedata> map =NewThreadlocal<mythreadscopedata>(); $     //private static Mythreadscopedata instance = null;//new Mythreadscopedata (); the      the     PrivateMythreadscopedata () {}; the     /** the * This can be used without synchronized, because it is located in different lines range are stored in threadlocal -      * @return in      */ the      Public Static /*synchronized*/mythreadscopedata getthreadinstance () { theMythreadscopedata instance =map.get (); About         //This approach is prone to multithreading concurrency problems. So it's best to use a hungry man mode the         if(instance==NULL){ theInstance =NewMythreadscopedata (); the Map.set (instance); +         } -         returninstance; the     }Bayi      the     PrivateString name; the     Private intAge ; -      - //Public Mythreadscopedata (String name, int.) { the //super (); the //this.name = name; the //this.age = age; the //    } -      PublicString GetName () { the         returnname; the     } the      Public voidsetName (String name) {94          This. Name =name; the     } the      Public intGetage () { the         returnAge ;98     } About      Public voidSetage (intAge ) { -          This. Age =Age ;101     }102}

6.ThreadLocal Class and application skills

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.