There are two ways in which multiple threads can access shared objects and data:
1, each thread executes the same code, for example, selling tickets: Multiple windows at the same time to sell the 100 tickets, the 100 tickets require multiple threads to share.
2, each thread executes different code, such as: the design of four threads, of which two threads each time to J 1, the other two threads each to J 1 reduction.
A, if each thread executes the same code, you can use the same Runnable object, which has shared data. Selling tickets can do this, each window is doing a sales ticket task, selling tickets are the same data (click to see the specific case).
b, if each thread executes different code, you need to use a different runnable object, there are two ways to achieve data sharing between runnable objects:
1. The shared data is encapsulated into an object separately, and the methods of manipulating the shared data are provided in the object, which can facilitate the mutual exclusion and communication of the operation of the shared data.
2, the various runnable objects as the internal class of a class, shared data as a member of the external class variables, the operation of the shared data is also provided in the external class, in order to achieve mutual exclusion and communication, the internal class of the Runnable object calls the external class to manipulate the method of sharing data.
Package com.ljq.test.thread; /** * The way to share data among multiple threads * Design four threads, where two threads add 1 to J each time, and two threads reduce the J 1 at a time.
Cycle 100 times.
* * @author Administrator */public class Multithreadsharedata {private static Sharedata data1 = new Sharedata ();
public static void Main (string[] args) {sharedata data2 = new Sharedata ();
New Thread (New Decrementrunnable (DATA2)). Start ();
New Thread (New Incrementrunnable (DATA2)). Start ();
Final Sharedata data1 = new Sharedata ();
New Thread (New Runnable () {@Override public void run () {data1.decrement ();
}). Start ();
New Thread (New Runnable () {@Override public void run () {data1.increment ();
}). Start (); /** * Create thread class, responsible for reduction of J 1 * * @author Administrator * */class Decrementrunnable implements Runnable {private Sh
Aredata data;
Public decrementrunnable (Sharedata data) {this.data = data; public void Run () {for (int i=0; i<100; i++) {DATA.DEcrement (); /** * Create thread class, responsible for J add 1 * * @author Administrator * */class Incrementrunnable implements Runnable {PR
Ivate sharedata data;
Public incrementrunnable (Sharedata data) {this.data = data;
public void Run () {for (int i=0; i<100; i++) {data.increment ();
}}/** * encapsulates shared data * * @author Administrator * */class Sharedata {private int j = 0;
/** * Add 1 */public synchronized void increment () {j + + each time to J;
System.out.println ("j++=" +j);
/** * Reduced by 1/public synchronized void decrement () {j--each time;
System.out.println ("j--=" +j);
}
}
Note: Several pieces of code to synchronize mutexes are best placed in separate methods, which are placed in the same class, which makes it easier to synchronize mutual exclusion and communication between them.