Requirements: Design 4 threads, where two threads add 1 to J each time, and two threads reduce J by 1.
Several ways to achieve data sharing: 1. Use the same Runnable object
If each thread executes the same code, the same Runnable object can be used, and the runnable has that shared data, for example, the ticket-selling system does.
As shown in the following example:
...
public static void Main (string[] args) {
ShareData1 shareData1 = new ShareData1 ();
New Thread (SHAREDATA1). Start ();
New Thread (SHAREDATA1). Start ();
Static Class ShareData1 implements Runnable {public
int count =;
public void Run () {
count--;
System.out.println ("Run:" +count);
}
...
2. Use a different runnable object
If each thread executes a different code, use a different runnable object, like the next two ways to implement data sharing between Runnable objects 1. Implements two Runnable objects, passing the shared data to two different threads, respectively.
....
public static void Main (string[] args) {
final ShareData1 shareData1 = new ShareData1 ();
New Thread (New MyRunnable1 (SHAREDATA1)). Start ();
New Thread (New MyRunnable1 (SHAREDATA1)). Start ();
Static class MyRunnable1 implements runnable{
private ShareData1 shareData1;
public void Run () {
} public
MyRunnable1 (ShareData1 shareData1) {
this.sharedata1 = shareData1;
}
}
Static class MyRunnable2 implements runnable{
private ShareData1 shareData1;
public void Run () {
} public
MyRunnable2 (ShareData1 shareData1) {
this.sharedata1 = shareData1;
}
}
Static class ShareData1 {
...
}
......
2. Take these runnable objects as an inner class and share the data as member variables.
This method solves the above requirements, please see the following code:
Package com.amos.concurrent;
public class Multithreadsharedata {private int J;
public static void Main (string[] args) {Multithreadsharedata multithreadsharedata = new Multithreadsharedata (); for (int i=0;i<2;i++) {new Thread (Multithreadsharedata.new ShareData1 ()). Start ();//Add New
Thread (Multithreadsharedata.new ShareData2 ()). Start ();//Decrease}}//self-add private synchronized void Inc () {
j + +;
System.out.println (Thread.CurrentThread (). GetName () + "inc" +J);
}//self-reduction private synchronized void Dec () {j--;
System.out.println (Thread.CurrentThread (). GetName () + "Dec" +j);
Class ShareData1 implements Runnable {public void run () {for (int i=0;i<5;i++) {
Inc. (); Class ShareData2 implements Runnable {public void run () {for (int i=0;i<5;i+
+) {Dec ();
} }
}
}
Effect:
Note:
1. The above code, first of all, defines a global variable J, that is, the shared data, then implements the Runnable object, respectively, to do the self increment and decrement operation, and then to the implemented Runnable object as an inner class to the new thread; last loop two times, Implements two self-subtraction and two self-increasing threads.
2. It should be noted here that the reason for the increase and self reduction is to facilitate thread safety control.
3. The difference between method two and method is that the method first assigns the shared data to the Runnable object, and the second method is to place the data as a global variable and then operate.