Threadlocal implementing thread-scoped shared variables

Source: Internet
Author: User

I. How to understand the shared data within a thread range

1, static int num=0;

2. Thread 1 accesses the NUM variable and is set to num=2; thread 2 accesses the NUM variable and is set to num=3;

3, when the object A, B, C in thread 1 accesses the NUM variable in thread 1, it is not the value set by itself, how can I get thread 1 to access the data set by it itself?

 Public classSharedata {Private Static DoubleNum=0;  Public Static voidMain (string[] args) {//There are 2 threads at a time         for(inti=0;i<2;i++){            NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub//each thread changes num's datanum=NewRandom (). nextdouble (); System.out.println (Thread.CurrentThread (). GetName ()+ "has a put num:" +num); //call the objects of A, B, respectively                    NewA (). get (); NewB (). get ();        }}). Start (); }    }    //A object outputs the data for NUM in the current thread    Static classa{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---A:" +num); }    }    //the B object outputs the data for NUM in the current thread    Static classb{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---B:" +num); }    }}

Results Analysis:

Second, using the map collection to implement thread-wide shared data

1, private static map<thread,double> map=new hashmap<thread,double> ();

Use the Map collection to maintain the individual variables in each thread.

 Public classThreadscopesharenum {//global shared variable num    Private Static DoubleNum=0; //put the thread as a keyword, set the value of NUM as the value put into the map collection    Private StaticMap<thread,double> map=NewHashmap<thread,double>();  Public Static voidMain (string[] args) {//maintenance of two threads         for(inti=0;i<2;i++){            NewThread (NewRunnable () {@Override Public voidrun () {//set the value of NUM and save it to CurrentThread                    Doublenum=NewRandom (). nextdouble ();                    Map.put (Thread.CurrentThread (), num); System.out.println (Thread.CurrentThread (). GetName ()+ "has a put num:" +num); //A, b objects Get values by CurrentThread                    NewA (). get (); NewB (). get ();        }}). Start (); }    }    //A object outputs the data for NUM in the current thread        Static classa{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---A:" +Map.get (Thread.CurrentThread ())); }        }        //the B object outputs the data for NUM in the current thread        Static classb{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---B:" +Map.get (Thread.CurrentThread ())); }        }}

Results Analysis:

Threadlocal shared variables that implement thread scope (1 x)
    1. new Threadlocal<t> (); A variable of type T is guaranteed to be shared correctly within the thread range.
    2. Public void Set (T value);//content maintains a map collection, and key defaults to the current thread.
    3. Public T get ();//Remove the value of the current thread, variable.
ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Random; Public classThreadscopesharenum {//global shared variable num    Private Static DoubleNum=0; //put the thread as a keyword, set the value of NUM as the value put into the map collection    Private StaticThreadlocal<double> map=NewThreadlocal<double>();  Public Static voidMain (string[] args) {//maintenance of two threads         for(inti=0;i<2;i++){            NewThread (NewRunnable () {@Override Public voidrun () {//set the value of NUM and save it to CurrentThread                    Doublenum=NewRandom (). nextdouble ();                    Map.set (num); System.out.println (Thread.CurrentThread (). GetName ()+ "has a put num:" +num); //A, b objects Get values by CurrentThread                    NewA (). get (); NewB (). get ();        }}). Start (); }    }    //A object outputs the data for NUM in the current thread        Static classa{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---A:" +map.get ()); }        }        //the B object outputs the data for NUM in the current thread        Static classb{ Public voidget () {System.out.println (Thread.CurrentThread (). GetName ()+ "has a num---B:" +map.get ()); }        }}
Iv. How to implement thread-wide through threadlocalmultipleShared variables

Threadlocal implementing thread-scoped shared variables

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.