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)
- new Threadlocal<t> (); A variable of type T is guaranteed to be shared correctly within the thread range.
- Public void Set (T value);//content maintains a map collection, and key defaults to the current thread.
- 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