尽管您保留了锁定顺序,但是如果将obj1与obj2切换,则会遇到死锁。
您必须寻找另一种解决方案来避免这种情况:锁定顺序+可选的打破平局锁定
int fromHash = System.identityHashCode(obj1);int toHash = System.identityHashCode(obj2);if (fromHash < toHash) { synchronized (obj1) { synchronized (obj2) { ........ } }} else if (fromHash > toHash) { synchronized (obj2) { synchronized (obj1) { ........ } }} else { synchronized (TIE_LOCK) { synchronized (fromAcct) { synchronized (toAcct) { ... } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)