4. threadlocal of data sharing within the thread range

Source: Internet
Author: User

1 /**2 * Data sharing for thread-scoped classes3 * Core: Threadlocal class4 * Actual scene:5 * Hibernate's Getcurrentsession method is to get the existing session from the thread range, create a new one and bind to the thread if it does not exist6 * Struts binds all parameters in a request to a thread-scoped object7  * @authorYzl8  *9  */Ten  Public classThreadpart_4 { One      Public Static voidMain (string[] args) { A         NewThread (NewRunnable () { -              Public voidrun () { -Mythreadlocalobj obj =mythreadlocalobj.getinstance (); the                  while(Obj.getdata () <20){ - Obj.add (); -System.out.println (Thread.CurrentThread (). GetName () + "After Add Method value:" +obj.getdata ()); -                     //This operation is actually the same Mythreadlocalobj object +                     NewMyadd (). Add (); -System.out.println (Thread.CurrentThread (). GetName () + "After Myadd Add Method value:" +obj.getdata ()); +                     Try { AThread.Sleep (1000); at}Catch(interruptedexception e) { - e.printstacktrace (); -                     } -                 } -             } - }). Start (); in          -         NewThread (NewRunnable () { to              Public voidrun () { +Mythreadlocalobj obj =mythreadlocalobj.getinstance (); -                  while(Obj.getdata () >0){ the obj.sub (); *System.out.println (Thread.CurrentThread (). GetName () + "after sub method value:" +obj.getdata ()); $                     //This operation is actually the same Mythreadlocalobj objectPanax Notoginseng                     Newmysub (). Sub (); -System.out.println (Thread.CurrentThread (). GetName () + "after MySub sub method value:" +obj.getdata ()); the                     Try { +Thread.Sleep (1000); A}Catch(interruptedexception e) { the e.printstacktrace (); +                     } -                 } $             } $ }). Start (); -     } - } the  - classmyadd{Wuyi      Public voidAdd () { the mythreadlocalobj.getinstance (). Add (); -     } Wu } -  About classmysub{ $      Public voidSub () { - mythreadlocalobj.getinstance (). Sub (); -     } - } A  + /** the * In single-case mode - * thread-wide sharing $ * Non-impact outside the thread range the  * @authorYzl the  * the  */ the classmythreadlocalobj{ -     Private intdata = 10; in      the      Public voidAdd () { thedata++; About     } the      the      Public voidSub () { thedata--; +     } -      the     Privatemythreadlocalobj () {};Bayi     Private StaticThreadlocal<mythreadlocalobj> local =NewThreadlocal<mythreadlocalobj>(); the      the      -      Public Staticmythreadlocalobj getinstance () { -         if(Local.get () = =NULL){ theLocal.set (Newmythreadlocalobj ()); the         } the         returnlocal.get (); the     } -  the      Public intGetData () { the         returndata; the     }94}

Operation Result:

Objects that operate on different threads are not the same, and the same thread operates on the same

1 Thread-0 After Add method value:112 Thread-1 After sub method value:93 Thread-0 After Myadd Add method Value:124 Thread-1 After mysub sub method Value:85Thread-1 after sub method value:76Thread-0 after Add method value:137Thread-1 after MySub sub method value:68Thread-0 after Myadd Add method value:149Thread-1 after sub method value:5TenThread-0 after Add method value:15 OneThread-1 after MySub sub method value:4 AThread-0 after Myadd Add method value:16 -Thread-0 after Add method value:17 -Thread-1 after sub method value:3 theThread-0 after Myadd Add method value:18 -Thread-1 after MySub sub method value:2 -Thread-0 after Add method value:19 -Thread-0 after Myadd Add method value:20 +Thread-1 after sub method value:1 -Thread-1 after MySub sub method value:0

4, thread-scoped data sharing 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.