この記事、または例では、マルチスレッド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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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 .
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は非常に詳細なので、読んでください。
ロックの問題に関するレポートを取得する最も簡単なメソッドを作成しましょう。
*このソースコードは、 ソースコードハイライターで強調表示されました。
- public static String reportDeadlocks( ArrayList <Long> knownDeadlocks){
- long [] threads = threadMXBean.findDeadlockedThreads();
- if ((threads == null )||(threads.length == 0)){
- nullを 返し ます 。
- }
- final ArrayList <Long> dumpIDs = new ArrayList <Long>(threads.length);
- for ( 長いスレッド:スレッド){
- boolean found = false ;
- if (knownDeadlocks!= null ){
- イテレーターitr = knownDeadlocks.iterator();
- while ((!found)&&(itr.hasNext())){
- found = thread ==((Number)itr.next())。longValue();
- }
- }
- (見つかった) 継続する場合
- dumpIDs.add(スレッド);
- }
- if (dumpIDs.size()== 0) nullを 返す ;
- if (knownDeadlocks!= null )knownDeadlocks.addAll(dumpIDs);
- ThreadDumpFilter filter = new ThreadDumpFilter(){
- public boolean include(スレッドt){
- long id = t.getId();
- for (Long dumpID:dumpIDs){
- if (dumpID == id) trueを 返し ます 。
- }
- falseを 返し ます 。
- }
- public String toString(){
- return String .format( "Deadlocked threads:%s" 、dumpIDs);
- }
- };
- StringWriter writer = new StringWriter ();
- ThreadDump.print( 新しい PrintWriter(ライター)、フィルター);
- writer.flush();
- return String .format( 「%d個の新しいデッドロックスレッドが見つかりました(%d合計デッドロック):\ n%s」 、
- dumpIDs.size()、threads.length、writer.toString());
- }
threadMXBean変数には、 ThreadMXBean Beanのインスタンスが含まれています。 コードの2行目では、ロックに問題のあるスレッド識別子のリストを取得します。 その後、(ソース内の)ThreadDumpヘルパークラスを使用して、問題のフローに関する情報を表示します。 ここで、アプリケーションの問題に関する情報を収集するための別のストリームが必要です。 そのコードは非常に簡単です:
*このソースコードは、 ソースコードハイライターで強調表示されました。
- プライベート 静的 クラス DeadlockTrackerThreadはThread {
- private volatile boolean checking = true ;
- DeadlockTrackerThread(){
- super( "デッドロック追跡スレッド" );
- setPriority(1);
- setDaemon( true );
- }
- public void setChecking(boolean b){
- this .checking = b;
- if ( this .checking) return ;
- 割り込み();
- }
- public void run(){
- ArrayList <Long> knownDeadlocks = new ArrayList <Long>();
- {
- while ( this .checking){
- 文字列レポート= ThreadUtils.reportDeadlocks(knownDeadlocks);
- if (report!= null ){
- システム out .print( 「デッドロックが検出されました」 +レポート);
- }
- {
- Thread.sleep(6000L);
- } catch (InterruptedException intx){}
- }
- } catch (例外e){
- System.err.print( "エラー:" + e.getMessage());
- }
- }
- }
6秒ごとに問題レポートをチェックし、そうであれば、System.outに表示します。
最初のプログラムに数行のコードを追加して、この一連のコードが機能することを確認します。
*このソースコードは、 ソースコードハイライターで強調表示されました。
- パブリック クラス TestDeadLockTracker {
- public static void main( String [] args){
- if (!ThreadUtils.isDeadlockDetectionSupported()){
- システム out .println( 「DeadlockDetectionはサポートされていません」 );
- 帰る
- }
- ThreadUtils.setDeadlockDetectionEnabled( true );
- ThreadUtils.setTrackingDeadlocksEnabled( true );
- final Object lock1 = new Object();
- ....
- }
- }
プログラムを起動すると、プログラムのコンソールに次の行が表示されます。
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)
-スレッドダンプの終了
ソースコード
ご清聴ありがとうございました。