Thread Range threadlocal

Source: Internet
Author: User

Thread-scoped shared variables, in layman's words, refer to specific threads that correspond to specific data and do not change as the object changes data.

The use of ThreadLocal is convenient for us to manage different data for different threads, and to be able to reuse the singleton, because we usually have different data objects corresponding to different singleton to save, if once the classification data too much, then we have to create a large number of singleton to save. However, threalocal can be reused in a single case. Below, please see the code

public class Threadscopsharedata {static threadlocal<integer> x= new threadlocal<integer> (); private static int data = 0;//private static map<thread,integer> threaddata = new Hashmap<thread, integer> ()///here for the same thread to get the common data public static void main (string[] args) {for (int i = 0;i<2;i++) {New Thread (new Runnable () {@Over ridepublic void Run () {//TODO auto-generated method Stubint data = new Random (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" +data); X.set (data) Threaddata.put (Thread.CurrentThread (), data); Mythreadscopedata.getinstance (). SetName ("name" +data);//Instance mythreadscopedata.getinstance () corresponding to this thread. Setage (data); New A (). get (); new B (). get ();}}). Start ();}} Static class A{public int get () {//int data = Threaddata.get (Thread.CurrentThread ()); int data = X.get ();//Gets the current thread data system. Out.println ("A from" +thread.currentthread (). GetName () + "Get Data:" +data); Mythreadscopedata MyData = Mythreadscopedata.getinstance (); System.ouT.println ("A from" +thread.currentthread (). GetName () + "Get Mydata:" +mydata.getage () + "," +mydata.getname ()); return     Data;}} Static class B{public int get () {int data = X.get ();//gets current thread data//int = Threaddata.get (Thread.CurrentThread ()); System.out.println ("B from" +thread.currentthread (). GetName () + "Get Data:" +data); Mythreadscopedata MyData = Mythreadscopedata.getinstance (); System.out.println ("B from" +thread.currentthread (). GetName () + "Get Mydata:" +mydata.getage () + "," +mydata.getname ()) ; return data;}}} Class Mythreadscopedata{//private static Mythreadscopedata instance=null;private mythreadscopedata () {}public static/ *synchronized*/Mythreadscopedata getinstance () {//Plus sync lock is to prevent multiple objects from appearing mythreadscopedata instance = Map.get ();// This can be used to improve the code reuse if (instance ==null) {instance = new Mythreadscopedata (), Map.set (instance), for the singleton corresponding to different thread new;} return instance;} private static threadlocal<mythreadscopedata> map = new threadlocal<mythreadscopedata> ();p rivate String name;private int Age;public StringGetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

The code runs as a result of


Visible objects are different, as long as the data is the same as the thread.

This gives us some hints on how to handle concurrent access.

Thread Range 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.