/* * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ class MasterLock { Object a, b; // classic deadlock between 2 methods // 1st method has a and wants b void oneWayBad() { synchronized (a) { synchronized (b) { } } } // 2nd method has b and wants a void theOtherWayBad() { synchronized (b) { synchronized (a) { } } } Object master, x, y; // both methods hold the master lock so cannot interleave // and thus cannot deadlock void oneWayOk() { synchronized (master) { synchronized (x) { synchronized (y) { } } } } void theOtherWayOk() { synchronized (master) { synchronized (y) { synchronized (x) { } } } } }