1.public class MultiThreadShareData {
private static ShareData1 data1=new ShareData1();
public static void main(String[] args) {
ShareData1 data1 = new ShareData1();
new Thread(new MyRunnable1(data1)).start();
new Thread(new MyRunnable2(data1)).start();
}
}
class MyRunnable1 implements Runnable{
private ShareData1 data1;
public MyRunnable1(ShareData1 data1) {
this.data1 = data1;
}
@Override
public void run() {
data1.decrement();
}
}
class MyRunnable2 implements Runnable{
private ShareData1 data1;
public MyRunnable2(ShareData1 data1) {
this.data1 = data1;
}
@Override
public void run() {
data1.increment();
}
}
class ShareData1{
private int j=0;
public synchronized void increment(){
j++;
}
public synchronized void decrement(){
j--;
}
}
作者:IT小白_L
来源:CSDN
原文:https://blog.csdn.net/lu930124/article/details/51251809
版权声明:本文为博主原创文章,转载请附上博文链接!