You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
891 B

/*
* 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) {
}
}
}
}
}