Suppose there is a public variable data, there are different threads can go to manipulate it, if the different threads to the data operation is completed before fetching this information, then there will be a thread of confusion between the problem, because a thread before fetching the data of the B thread and then modify it, Let's write a program to illustrate the problem:
Public class threadscopesharedata { Private Static intdata =0;//Public data Public Static void Main(string[] args) { for(inti =0; I <2; i + +) {//Open Two threads NewThread (NewRunnable () {@Override Public void Run() {inttemp =NewRandom (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () +"has put a data:"+ temp);//print out to see the effectdata = temp;//Operation data: assigning new values NewTestA (). GetData ();NewTestb (). GetData (); }}). Start (); } }StaticClass TestA { Public void GetData() {System.out.println ("A get data from"+ Thread.CurrentThread (). GetName () +": "+ data);//Remove public data} }StaticClass Testb { Public void GetData() {System.out.println ("B get data from"+ Thread.CurrentThread (). GetName () +": "+ data); } }}
Check out the printed results:
Thread-0 has put a data:-1885917900
Thread-1 has put a data:-1743455464
A get data from Thread-0:1743455464
A get data from Thread-1:1743455464
B get data from Thread-1:1743455464
B get data from Thread-0:1743455464
As can be seen from the results, two times the value of data is really different, but the last two threads print out is the last assigned value, indicating that the data Thread-0 is wrong, this is the problem of sharing data between threads.
Of course, we can use the Synchronized keyword to set up a few lines of code in the Run () method so that each thread finishes its own work and prints out its own information, which is fine, and it does solve the problem of sharing data between the threads above. However, this is at the expense of other threads being blocked, that is, when Thread-0 is executing, Thread-1 is blocked and must wait for Thread-0 execution to execute.
So what if I want two threads to run at the same time and not affect the values they've taken out of each other? This is also the focus of this article, the idea is to solve the problem is: Although the public data is now operating, but the different threads themselves to the data to maintain a copy, this copy is not shared among the threads, but each thread is unique, so the data maintained in different threads is not the same, The last time it was taken, which thread I took the data from.
Based on this idea, I will make a change to the above procedure, as follows:
Public class threadscopesharedata { Private Static intdata =0;//Public data //define a map to store each thread and its corresponding data in a key-value pair, that is, Thread:data Private StaticMap<thread, integer> threaddata =NewHashmap<thread, integer> (); Public Static void Main(string[] args) { for(inti =0; I <2; i + +) {NewThread (NewRunnable () {@Override Public void Run() {inttemp =NewRandom (). Nextint (); System.out.println (Thread.CurrentThread (). GetName () +"has put a data:"+ temp);//print out to see the effectThreaddata.put (Thread.CurrentThread (), temp);//Store a copy of the data in this thread to the mapdata = temp;//Operation data: assigning new values NewTestA (). GetData ();NewTestb (). GetData (); }}). Start (); } }StaticClass TestA { Public void GetData() {System.out.println ("A get data from"+ Thread.CurrentThread (). GetName () +": "+ Threaddata.get (Thread.CurrentThread ()));//Remove the copy that each thread maintains} }StaticClass Testb { Public void GetData() {System.out.println ("B get data from"+ Thread.CurrentThread (). GetName () +": "+ Threaddata.get (Thread.CurrentThread ())); } }}
The above program maintains a map, the key value pair is the thread and its data, then when the operation of data, the respective data is saved to this map, so that each thread will save a certain difference, when the second fetch, according to the current thread object as key to remove the corresponding data copy, So that different threads don't interfere with each other. Take a look at the results:
Thread-0 has put a data:1817494992
Thread-1 has put a data:-1189758355
A get data from Thread-0:1817494992
B get data from Thread-1:1189758355
A get data from Thread-0:1817494992
B get data from Thread-1:1189758355
is the thread-wide sharing of data, that is, in the same thread, the data is shared, and the threads are not shared.
This reminds me of the use of the threadlocal when learning the database, the operation of the database needs to connection, if the current thread has to take the current thread in the connection, otherwise the new one put into the current thread, so there is no problem, Because each thread itself shares a connection, it is not shared between threads. This is also very well understood, this connection certainly cannot share, suppose A and B user all get this connection and open the transaction, now a starts to transfer, but the money is not good, b turn well closed the transaction, then a there is a problem.
The problem of sharing data within a thread is summed up so much.
-Willing to share and progress together!
-More articles please see: http://blog.csdn.net/eson_15
Shared data across Java concurrent threads