/* * 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. */ import java.util.concurrent.Executor; class Deadlock { // executors are injected and annotated as to what thread they schedule to @ForUiThread private final Executor mUiThreadExecutor = null; @ForNonUiThread private final Executor mNonUiThreadExecutor = null; Object monitorA, monitorB; // text-book deadlock between UI and background thread public void postDeadlockBad() { mUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorA) { synchronized (monitorB) { } } } }); mNonUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorB) { synchronized (monitorA) { } } } }); } Object monitorC, monitorD; // non-deadlock as both work items are scheduled on same thread public void postOnUIThreadOk() { mUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorC) { synchronized (monitorD) { } } } }); mUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorD) { synchronized (monitorC) { } } } }); } Object monitorE, monitorF; // deadlock as both work items are scheduled on background threads public void postOnBGThreadBad() { mNonUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorE) { synchronized (monitorF) { } } } }); mNonUiThreadExecutor.execute( new Runnable() { @Override public void run() { synchronized (monitorF) { synchronized (monitorE) { } } } }); } }