If multiple threads are using the same data, then how to guarantee the data sharing within the thread scope.
We can use a map to store the current thread and its data as follows:
Package Andy.thread.traditional.test;import Java.util.hashmap;import java.util.map;import java.util.Random;/** * @ Author Zhang,tianyou * @version November 8, 2014 2:12:44 shared data in thread-wide *///public class Threadscopeshareddata {private static int Data = 0;//threadlocal provides for a thread-wide share of a datastore that can only store one data private static Map<thread, integer> threaddata = new Hashmap<thre AD, integer> ();//private static threadlocal<integer> x = new threadlocal<integer> ();p ublic static void Main (string[] args) {for (int i = 0; i < 2; i++) {New Thread (new Runnable () {@Overridepublic void run () {int data = new Random (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); Threaddata.put (Thread.CurrentThread (), data);//x.set (data); New A (). get (); new B (). get ();}}). Start ();}} Static class A {public void get () {int data = Threaddata.get (Thread.CurrentThread ());//int data = X.get (); System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Get data:" + data);}} Static Class B {public void get () {int data = Threaddata.get (Thread.CurrentThread ());//int data = X.get (); System.out.println ("B from" + Thread.CurrentThread (). GetName () + "Get data:" + data);}}
The results are as follows:
Thread-1 has put data: -591164568thread-0 have put data:2032497041a from Thread-0 get data:2032497041a from Thread-1 get Data: -591164568b from Thread-0 get data:2032497041b from Thread-1 get data: 591164568
But the JDK also provides us with another way to implement it: The Threadlocal class, which implements the respective thread local variables and is independent of the initialization copy of the variable.
-
Threadlocal<t>
-
Extends Object
This class provides thread-local (thread-local) variables. These variables are different from their ordinary counterparts, because each thread that accesses a variable (through its get or set method) has its own local variable, independent of the initialized copy of the variable. ThreadLocal instances are typically private static fields in a class that want to associate the state with a thread (for example, a user ID or transaction ID).
For example, the following class generates a local identifier that is unique to each thread. The thread ID is assigned at the first call to Uniquethreadidgenerator.getcurrentthreadid () and will not change in subsequent calls.
Import Java.util.concurrent.atomic.AtomicInteger; public class Uniquethreadidgenerator { private static final Atomicinteger uniqueId = new Atomicinteger (0); private static final ThreadLocal < integer > uniquenum = new ThreadLocal < Integer > () { @Override PR otected Integer InitialValue () { return uniqueid.getandincrement (); } }; public static int GetCurrentThreadID () { return uniqueid.get ();
Each thread maintains an implicit reference to a copy of its thread's local variables, as long as the thread is active and the ThreadLocal instance is accessible, and all copies of its thread-local instance are garbage-collected after the threads have disappeared (unless there are other references to those replicas).
The more elegant implementation of the above code is as follows:
Package Andy.thread.traditional.test;import java.util.random;/** * @author zhang,tianyou * @version November 8, 2014 PM 2 : 26:24 */public class Threadlocaltest {private static int data = 0;//ThreadLocal provides thread-wide data sharing public static void main (STR Ing[] args) {for (int i = 0; i < 2; i++) {New Thread (new Runnable () {@Overridepublic void run () {int data = new Random ( ). Nextint (); System.out.println (Thread.CurrentThread (). GetName () + "has put data:" + data); Mythreadscopedata.getthreadscopeinstance (). SetName ("name =" + data); Mythreadscopedata.getthreadscopeinstance (). setage (data); new A (). get (); new B (). get ();}}). Start ();}} Static class A {public void get () {Mythreadscopedata mythreadscopedata = mythreadscopedata.getthreadscopeinstance (); System.out.println ("A from" + Thread.CurrentThread (). GetName () + "Name:" + mythreadscopedata.getname () + "age =" + Mythr Eadscopedata.getage ());}} Static class B {public void get () {Mythreadscopedata mythreadscopedata = mythreadscopedata.getthreadscopeinstance (); SysteM.out.println ("B from" + Thread.CurrentThread (). GetName () + "Name:" + mythreadscopedata.getname () + "age =" + MYTHREADSC Opedata.getage ());}}} Class Mythreadscopedata {private Mythreadscopedata () {}private static threadlocal<mythreadscopedata> map = new Threadlocal<mythreadscopedata> ()///This is not required here synchronized each thread should operate its own data public static Mythreadscopedata Getthreadscopeinstance () {//Returns the value in the current thread copy of this thread's local variable. Mythreadscopedata instance = Map.get (), if (instance = = null) {instance = new Mythreadscopedata ();//sets the value in the current thread copy of this thread local variable to Specify a value. Map.set (instance);} return instance;} private string Name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
It works as follows:
Thread-1 have put data:1322483490a from Thread-1 name:name = 1322483490age = 1322483490B from Thread-1 name:name = 13224 83490age = 1322483490thread-0 have put data:1149801771a from Thread-0 name:name = 1149801771age = 1149801771B from Thread -0 name:name = 1149801771age = 1149801771
Data sharing across multiple thread threads threadlocal