13. TLW_TWO_LOCK_WAIThtml
Waiting on a monitor while two locks are held may cause deadlock. Performing a wait only releases the lock on the object being waited on, not any other locks. This not necessarily a bug, but is worth examining closely.java
關於這個問題,咱們迴歸下基礎,web
Tanenbaum的《現代操做系統》裏這麼解釋的死鎖:oracle
A deadlock occurs when a set of processes are blocked waiting for an event that only some other process in the set can cause. On the other hand, processes in a livelock are not blocked. Instead, they continue to execute checking for a condition to become true that will never become true. Thus, in addition to the resources they are holding, processes in livelock continue to consume precious CPU time. Finally, starvation of a process occurs because of the presence of other processes as well as a stream of new incoming processes that end up with higher priority that the process being starved. Unlike deadlock or livelock, starvation can terminate on its own, e.g. when existing processes with higher priority terminate and no new processes with higher priority arrive.
湯子贏的《計算機操做系統》裏會這麼教咱們:ide
四個必要條件:互斥條件,請求和保持條件,不剝奪條件和環路等待條件.spa
處置方案:預防死鎖,避免死鎖,檢測死鎖和解除死鎖;操作系統
舉個死鎖的例子:.net
public class TestLock implements Runnable { public int flag; public static Object o1 = new Object(); public static Object o2 = new Object(); public static void main(String[] args) { TestLock tl1 = new TestLock(); TestLock tl2 = new TestLock(); tl1.flag = 1; tl2.flag = 0; Thread t1 = new Thread(tl1); Thread t2 = new Thread(tl2); t1.start(); t2.start(); } @Override public void run() { if (flag == 0) { synchronized (o1) { System.out.println("o1 lock"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.out.println("o2 lock"); } } } if (flag == 1) { synchronized (o2) { System.out.println("lock o2"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o1) { System.out.println("lock o1"); } } } } }
生產環境比較常見的問題吧,固然報錯這種問題應該code review時就排除掉了,主要說那些隱藏的死鎖,Performance Team會遇到這種狀況多一些。code
通常入門分析的話,看看jstack就能夠上手了,orm
借一個圖說明問題: