古龙的编码风格
如果古龙是一个程序员的话
他的编码风格或许会是这样的
package liveness;//他已经记不得是什么时候写的这段代码public class Transfer{private static final Object tieLock = new Object(); //每个程序员都会有感到不安时候public void transferMoney( Account from, Account to, int amount ){synchronized(from){synchronized(to){if((from.getBalance()-amount)<0){from.debit(amount);to.credit(amount);}}}}//这里他感到了些许的安全public void transferMoneySafe( final Account from, final Account to, final int amount ){class Helper{public void transfer(){if((from.getBalance()-amount)<0){from.debit(amount);to.credit(amount);}}}int fromHash = System.identityHashCode(from);int toHash = System.identityHashCode(to);if(fromHash < toHash){synchronized(from){synchronized(to){new Helper().transfer();}}}else if(fromHash < toHash){synchronized(to){synchronized(from){new Helper().transfer();}}}else{synchronized(tieLock){synchronized(to){synchronized(from){new Helper().transfer();}}}}}}