注意上面在Account的deposit和withdraw方法中之所以要把对amount的运算使用一个临时变量首先存储sleep一段时间然后再赋值给amount是为了模拟真实运行时的情况因为在真实系统中账户信息肯定是存储在持久媒介中比如RDBMS中此处的睡眠的时间相当于比较耗时的数据库操作最后把临时变量tmp的值赋值给amount相当于把amount的改动写入数据库中运行AccountTest结果如下(每一次结果都会不同)
E:javaexerin>java AccountTest
Finally Johns balance is:
E:javaexerin>java AccountTest
Finally Johns balance is:
E:javaexerin>java AccountTest
Finally Johns balance is:
E:javaexerin>java AccountTest
Finally Johns balance is:
E:javaexerin>java AccountTest
Finally Johns balance is:
E:javaexerin>java AccountTest
Finally Johns balance is:
[] [] []