Data sharing within threads is independent of objects, for example: John give Lee four money, open a thread to carry out the movement of money, just at the same time Harry to Zhao Liu transfer money, open B thread to carry out the transfer of money, because it is called the same action or the object, so if you can not guarantee the object between threads, then it is very likely to happen, John give Lee four turn the money to transfer the Harry to the Zhao Liu of money, and Harry turn money the whole action has not completed, then caused the turn money error, so on one hand to ensure data sharing, on the other hand to ensure the object of the opposition. 1. Use map to encapsulate objects for sharing data
Package com.amos.concurrent;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Random; /** * @ClassName: Threadscopesharedata * @Description: The following example uses the Map object to share the data * @author: Amosli * @email: Hi_amos@outlook . com * @date APR, 2014 6:19:02 PM */public class Threadscopesharedata {public static map<object, integer> Map
= new Hashmap<object, integer> ();
public static void Main (string[] args) {for (int i = 0; i < 3; i++) {New Thread (new Runnable () { public void Run () {int data = new Random (). Nextint ()//Set value to data, S
Ystem.out.println (Thread.CurrentThread (). GetName () + "Set data:" + data);
Map.put (Thread.CurrentThread (), data);//The value according to thread to set the value, take the time also according to thread to take to ensure that the data sharing, but also to ensure the independence of the object.
New A (). get ();
New B (). get ();
}). Start (); } static Class A {//here A and B methods are the same, here is to indicate the possibility of calling differentobject to perform data operations public int get () {data = Map.get (Thread.CurrentThread ());
System.out.println ("A From Thread:" + thread.currentthread (), getName () + "is" + data);
return data;
} static class B {public int get () {int data = Map.get (Thread.CurrentThread ());
System.out.println ("B from Thread:" + thread.currentthread (). GetName () + ' is "+ data);
return data; }
}
}
Operation Effect:
2. Use threadlocal to realize data sharing
Create a ThreadLocal, you can directly new out, its set value support generic type, new THREADLOCAL<T>, the following code rewrite:
public class Threadlocalsharedata {private static threadlocal<integer> Threadlo
Cal = new threadlocal<integer> ();
public static void Main (string[] args) {for (int i = 0; i < 3; i++) {New Thread (new Runnable () { public void Run () {int data = new Random (). Nextint ()//Set value to data, S
Ystem.out.println (Thread.CurrentThread (). GetName () + "Set data:" + data);
Threadlocal.set (data);//Use threadLocal to set the value new A (). get ();
New B (). get ();
}). Start (); } static Class A {//here the methods of A and B are the same, here is to indicate that it is possible to invoke different objects to perform data operations public int get () {int = thr
Eadlocal.get ();
System.out.println ("A From Thread:" + thread.currentthread (), getName () + "is" + data);
return data; } Class B ...}
The following is the source code for the threadlocal set (T value) method:
public void Set (t value) {
Thread t = thread.currentthread ();
Threadlocalmap map = getmap (t);
if (map!= null)
Map.set (this, value);
else
createmap (t, value);
This is also used to set the value of the map method, but also encapsulates a layer of threadlocalmap.
View the source code for its threadlocal get () method:
Public T get () {
Thread t = thread.currentthread ();
Threadlocalmap map = getmap (t);
if (map!= null) {
Threadlocalmap.entry e = Map.getentry (this);
if (e!= null) return
(T) e.value;
}
return Setinitialvalue ();
}
The same is done by binding to the thread and taking the value.
3. Instance Test
package com.amos.concurrent; class Account {/* * defines a variable of type threadlocal, which is a thread-local variable
Amount/Private threadlocal<string> name = new Threadlocal<string> ();
Defines a constructor public account (String str) {this.name.set (str) that initializes the Name property;
The following code is used to access the value of the name copy of the current thread System.out.println ("------" + this.name.get ());
}//Name Getter,setter method public String GetName () {return name.get ();
public void SetName (String str) {this.name.set (str);
Class MyTest extends Thread {//define an account attribute private account account;
Public MyTest (account account, String name) {super (name);/Set thread name This.account = account; @Override public void