public class SaveGetMoney implements Runnable{
private static int deposit = 10000;
private byte flag;
private int dynamicMoney;
public SaveGetMoney(byte flag,int dynamicMoney) {
super();
this.flag = flag;
this.dynamicMoney = dynamicMoney;
}
public void save(int saveCount){
deposit = deposit + saveCount;
System.out.println("public void save");
}
public void get(int getCount){
deposit = deposit - getCount;
System.out.println("public void get");
}
@Override
public void run() {
if(flag == 1){
System.out.println(Thread.currentThread().getName()+" savesavesave: "+deposit);
save(dynamicMoney);
System.out.println("当前执行了: "+Thread.currentThread().getName()+"去save了: "+dynamicMoney);
}
else if(flag == 0){
System.out.println(Thread.currentThread().getName()+" getgetget: "+deposit);
get(dynamicMoney);
System.out.println("当前执行了: "+Thread.currentThread().getName()+"去get了: "+dynamicMoney);
}
}
public static void main(String[] args) {
System.out.println("刚开始的static的deposit: "+deposit);
new Thread(new SaveGetMoney((byte)1,1000)).start();
new Thread(new SaveGetMoney((byte)1,500)).start();
System.out.println("当前执行了: "+Thread.currentThread().getName()+": "+deposit);
}
}
得到的结果为:
刚开始的static的deposit: 10000
Thread-0 savesavesave: 10000
public void save
当前执行了: Thread-0去save了: 1000
当前执行了: main: 10000
Thread-1 savesavesave: 11000
public void save
当前执行了: Thread-1去save了: 500
问题在于:“当前执行了: main: 10000” 这个是为什么啊?我觉得应该是11000啊,不对吗?