実行中のアプリケーションのDeadLocksに関するレポート

こんにちは、ハラジテル!

この記事、または例では、マルチスレッドJavaアプリケーションのロックされたスレッドに関する情報を取得する方法を示したいと思います。 カットの下では、最も単純なDeadLockの状況の例と、それに関する情報を取得する方法が提示されます。



DeadLoackが受信される最も単純な状況は、あるスレッドが別のスレッドによって既にロックされているリソースをロックしようとした場合です。

public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  1. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  2. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  3. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  4. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  5. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  6. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  7. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  8. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  9. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  10. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  11. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  12. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  13. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  14. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  15. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  16. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  17. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  18. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  19. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  20. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  21. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  22. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  23. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  24. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  25. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  26. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  27. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  28. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  29. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  30. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  31. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  32. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  33. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  34. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  35. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  36. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  37. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  38. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  39. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  40. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  41. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  42. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  43. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



  44. public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .



public class TestDeadLockTracker { public static void main( String [] args) { final Object lock1 = new Object(); final Object lock2 = new Object(); Thread t1 = new Thread( "Deadlock Test T1" ) { public void run() { synchronized (lock1) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock2) { try { Thread.sleep(30L); } catch (Throwable ignored) { } lock2.notify(); } lock1.notify(); } } }; Thread t2 = new Thread( "Deadlock Test T2" ) { public void run() { synchronized (lock2) { try { Thread.sleep(150L); } catch (Throwable ignored) { } synchronized (lock1) { try { Thread.sleep(300L); } catch (Throwable ignored) { } lock1.notify(); } lock2.notify(); } } }; t1.start(); t2.start(); } } * This source code was highlighted with Source Code Highlighter .





この例では、両方のスレッドがリソースlock1およびlock2によってそれらの間でしっかりとロックされます。

プログラムを終了することはありません。 バージョンjava 1.5以降では、このような状況をキャッチするのは非常に簡単です。このため、スレッドサブシステムThreadMXBeanと対話するための特別なBeanがあります。 Javadocは非常に詳細なので、読んでください。

ロックの問題に関するレポートを取得する最も簡単なメソッドを作成しましょう。





  1. public static String reportDeadlocks( ArrayList <Long> knownDeadlocks){
  2. long [] threads = threadMXBean.findDeadlockedThreads();
  3. if ((threads == null )||(threads.length == 0)){
  4. nullを 返し ます
  5. }
  6. final ArrayList <Long> dumpIDs = new ArrayList <Long>(threads.length);
  7. for長いスレッド:スレッド){
  8. boolean found = false ;
  9. if (knownDeadlocks!= null ){
  10. イテレーターitr = knownDeadlocks.iterator();
  11. while ((!found)&&(itr.hasNext())){
  12. found = thread ==((Number)itr.next())。longValue();
  13. }
  14. }
  15. (見つかった) 継続する場合
  16. dumpIDs.add(スレッド);
  17. }
  18. if (dumpIDs.size()== 0) nullを 返す ;
  19. if (knownDeadlocks!= null )knownDeadlocks.addAll(dumpIDs);
  20. ThreadDumpFilter filter = new ThreadDumpFilter(){
  21. public boolean include(スレッドt){
  22. long id = t.getId();
  23. for (Long dumpID:dumpIDs){
  24. if (dumpID == id) trueを 返し ます
  25. }
  26. falseを 返し ます
  27. }
  28. public String toString(){
  29. return String .format( "Deadlocked threads:%s" 、dumpIDs);
  30. }
  31. };
  32. StringWriter writer = new StringWriter ();
  33. ThreadDump.print( 新しい PrintWriter(ライター)、フィルター);
  34. writer.flush();
  35. return String .format( 「%d個の新しいデッドロックスレッドが見つかりました(%d合計デッドロック):\ n%s」
  36. dumpIDs.size()、threads.length、writer.toString());
  37. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


threadMXBean変数には、 ThreadMXBean Beanのインスタンスが含まれています。 コードの2行目では、ロックに問題のあるスレッド識別子のリストを取得します。 その後、(ソース内の)ThreadDumpヘルパークラスを使用して、問題のフローに関する情報を表示します。 ここで、アプリケーションの問題に関する情報を収集するための別のストリームが必要です。 そのコードは非常に簡単です:





  1. プライベート 静的 クラス DeadlockTrackerThreadはThread {
  2. private volatile boolean checking = true ;
  3. DeadlockTrackerThread(){
  4. super( "デッドロック追跡スレッド" );
  5. setPriority(1);
  6. setDaemon( true );
  7. }
  8. public void setChecking(boolean b){
  9. this .checking = b;
  10. ifthis .checking) return ;
  11. 割り込み();
  12. }
  13. public void run(){
  14. ArrayList <Long> knownDeadlocks = new ArrayList <Long>();
  15. {
  16. whilethis .checking){
  17. 文字列レポート= ThreadUtils.reportDeadlocks(knownDeadlocks);
  18. if (report!= null ){
  19. システム out .print( 「デッドロックが検出されました」 +レポート);
  20. }
  21. {
  22. Thread.sleep(6000L);
  23. } catch (InterruptedException intx){}
  24. }
  25. } catch (例外e){
  26. System.err.print( "エラー:" + e.getMessage());
  27. }
  28. }
  29. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


6秒ごとに問題レポートをチェックし、そうであれば、System.outに表示します。



最初のプログラムに数行のコードを追加して、この一連のコードが機能することを確認します。





  1. パブリック クラス TestDeadLockTracker {
  2. public static void main( String [] args){
  3. if (!ThreadUtils.isDeadlockDetectionSupported()){
  4. システム out .println( 「DeadlockDetectionはサポートされていません」 );
  5. 帰る
  6. }
  7. ThreadUtils.setDeadlockDetectionEnabled( true );
  8. ThreadUtils.setTrackingDeadlocksEnabled( true );
  9. final Object lock1 = new Object();
  10. ....
  11. }
  12. }
*このソースコードは、 ソースコードハイライターで強調表示されました。


プログラムを起動すると、プログラムのコンソールに次の行が表示されます。

Dedlockが検出されました2つの新しいデッドロックスレッドが見つかりました(合計2つのデッドロック):

-スレッドダンプデッドロックスレッド:[11、10]

スレッド[デッドロックテストT1、5、メイン]「デッドロックテストT1」Id = 10「デッドロックテストT2」が所有するjava.lang.Object@272d7a10でブロックId = 11



TestDeadLockTracker $ 1.run(TestDeadLockTracker.java:27)

スレッド[デッドロックテストT2.5、メイン]「デッドロックテストT2」Id = 11「Deadlock Test T1」が所有するjava.lang.Object@1aa8c488でブロックId = 10



TestDeadLockTracker $ 2.run(TestDeadLockTracker.java:45)

-スレッドダンプの終了



ソースコード

ご清聴ありがとうございました。



All Articles