How multiple threads access shared objects and data
1. If each thread executes the same code, the same Runnable object can be used, and the Runnable object has that shared data, for example, the ticket system can do so.
2. If the code executed by each thread is different, it is necessary to use different runnable objects in the following two ways to implement the data sharing between these runnable objects:
Encapsulates the shared data in another object, and then passes the object one at a to each Runnable object. Each thread's method of manipulating the shared data is also assigned to that object to complete, which makes it easy to achieve mutual exclusion and communication for each operation that is performed on that data.
These runnable objects are used as internal classes in a class, shared data as member variables in this external class, and each thread assigns an action method to the shared data to an external class to enable mutual exclusion and communication of the various operations on the shared data. These methods for external classes are called by each Runnable object that is an inner class.
The combination of the above two ways: the shared data is encapsulated in another object, each thread to the method of sharing the data is also assigned to the object to complete, the object as a member of the external class variables or local variables in the method, The Runnable object for each thread acts as a member inner class or local inner class in an external class.
In short, it is best to synchronize mutually exclusive pieces of code in a few separate methods, these methods are placed in the same class, it is easier to implement the synchronization of mutual exclusion and communication between them.
3. In an extreme and simple way, define a static variable in any class that will be shared by all threads (with caution).
Package Cn.itcast.lishehe;import Java.util.hashmap;import java.util.map;import java.util.random;/** 李社河-June 12, 2015 * Topic Requirements: Constructs two threads, requires: * (1) Two threads concurrent operation (this requires that the Syschronized keyword cannot be used) * (2) requires two threads to access their data separately, without interference (you can use the Map collection to Thread.currentthrea D () as key, with data as value) * (3) thread has a, b two modules, between the modules to share the **/public class Thread2threaddataindependent {static Map <Thread,Integer> Threadmap = new Hashmap<thread, integer> (); public static void Main (string[] args) {for (int i = 0; i < 2; i++) {New Thread (new Runnable () {@Override public void run () {int data = new Ra Ndom (). Nextint (); The data here must be defined as local variables, otherwise there is no independent System.out.println (Thread.CurrentThread ()) between threads. GetName () + "has put data: "+data); Threadmap.put (Thread.CurrentThread (), data); New A (). get (); New B (). get (); } }). Start (); }} static class a{public void Get () {int data = Threadmap.get (thread.currentthread ()); System.out.println ("A from" +thread.currentthread (). GetName () + "Get Data:" +data); }} static class b{public void Get () {int data = Threadmap.get (Thread.CurrentThread ()); System.out.println ("B from" +thread.currentthread (). GetName () + "Get Data:" +data); } } }
Run results
Multithreading (iv) implements shared data between modules in a thread-wide and inter-thread data Independence (Map collection)