public class MulitThreadShareData { public static void main(String[] args) {
final ShareData1 data1 = new ShareData1();
new Thread(new Runnable() {
public void run() {
data1.increment();
}
}).start(); new Thread(new Runnable() {
public void run() {
data1.decrement();
}
}).start(); new Thread(new MyRunnable1(data1)).start();
new Thread(new MyRunnable2(data1)).start(); }
} class ShareData1 {
private int j = 0; public void increment(){
for (int i = 0; i < 10; i++) {
j++;
System.out.println(j+" now incre ");
}
} public void decrement(){
for (int i = 0; i < 10; i++) {
j--;
System.err.println(j+" now decre ");
}
}
} class MyRunnable1 implements Runnable{ private ShareData1 data; public MyRunnable1(ShareData1 data){
this.data = data;
} public void run() {
data.decrement();
}
} class MyRunnable2 implements Runnable{ private ShareData1 data; public MyRunnable2(ShareData1 data){
this.data = data;
} public void run() {
data.increment();
}
}