jstack ã§ã¹ã¬ãããã³ããåã
jstack ã¨ãããã¼ã«ãããããã®ãã¼ã«ã¯ãç¾å¨å®è¡ä¸ã® Java ããã»ã¹ã®ã¹ã¬ããã®ç¶æ ãåå¾åºæ¥ããã¼ã«ã ã
æã£ãããã«æ§è½ãåºãªãæããããããã¯ã£ã½ãç¾è±¡ãçºçããéã¯ãã®ãã¼ã«ã使ã£ã¦ã¹ã¬ããã®ç¶æ ãåå¾ãã¤ã¾ãã¹ã¬ãããã³ããå¾ã¦ããããã解決ã®ç³¸å£ãæ¢ãã
ä»åã®ãµã³ãã«ã³ã¼ãã¯ä¸è¨ã³ã¼ãã
å°ãé·ãããThread1 㨠Thread2 ã¯ã©ã¹ã¯ã»ã¼åä¸ã ãéãã®ã¯ããªã½ã¼ã¹ãããã¯ããé åºãThread1 ã¯ã©ã¹ã¯ resource1, resource2 ã®é ã«ããã¯ããããThread2 ã¯ã©ã¹ã¯ resource2, resource1 ã®é ã«ããã¯ãããã¤ã¾ãããããï¼ã¤ã®ã¯ã©ã¹ãåæã«å®è¡ããã¨ãããããã¯ãèµ·ããã
public class DeadLockTest { public static Object resource1 = new Object(); public static Object resource2 = new Object(); public static void main(String[] args) { // Thread1, Thread2 ã¯ã©ã¹ã(ã»ã¼)åæã«å®è¡ new Thread(new Thread1()).start(); new Thread(new Thread2()).start(); } } // resource1, resource2 ã®é ã«ããã¯ãã class Thread1 implements Runnable { @Override public void run() { synchronized (DeadLockTest.resource1) { System.out.println("thread1 - locked resource1"); synchronized (DeadLockTest.resource2) { System.out.println("thread1 - locked resource2"); } } } } // resource2, resource1 ã®é ã«ããã¯ãã class Thread2 implements Runnable { @Override public void run() { synchronized (DeadLockTest.resource2) { System.out.println("thread2 - locked resource2"); synchronized (DeadLockTest.resource1) { System.out.println("thread2 - locked resource1"); } } } }
ãã¦ä¸è¨ DeadLockTest ã¯ã©ã¹ãå®è¡ããã¨ããããããããããã¯ãçºçãããã³ã³ã½ã¼ã«ã«ã¯ä¸è¨ã®ãããªåºåããããå¾ããã£ã¨ãªã½ã¼ã¹ã®è§£æ¾ãå¾ ã£ã¦ããç¶æ ã«é¥ãã(ã¿ã¤ãã³ã°ã«ãã£ã¦ã¯çºçããªãå ´åããã)
thread1 - locked resource1 thread2 - locked resource2 (ããã§ããã¯è§£æ¾å¾ ã¡ç¶æ )
ãã¦ãç¡äºãããããã¯ãåç¾ããã¨ããã§ããã¤ã®ã¹ã¬ãããã³ããåå¾ãã¦ã¿ãããjstack ã«ã¯å¼æ°ã«å¯¾è±¡ã® Java ããã»ã¹ã®ããã»ã¹ ID ã渡ãããªã®ã§ã¾ã㯠jps ã³ãã³ãã§å¯¾è±¡ã® Java ããã»ã¹ã®ããã»ã¹ ID ã調ã¹ããã
$ jps 12359 Jps 11367 org.eclipse.equinox.launcher_1.2.0.v20110502.jar 12316 DeadLockTest
ãã®çµæã¯ç°å¢ã«ãã£ã¦ç°ãªããããã®ãã¹ã以å¤ã« Java ããã»ã¹ãç«ã¡ä¸ãã¦ããªãå ´åã¯ä¸è¨ã®ãããªçµæã«ãªãã ããããã¦ããã㧠DeadLockTest ã® PID 㯠12316 ã ã¨å¤æããã
ã§ã¯ jstack ã§ã¹ã¬ãããã³ããåå¾ãããããã ãã®ããã°ã©ã ã§ãçµæã¯ãããªãã«é·ãã®ã§ä¸ç¥ããã
$ jstack 12316 2012-06-29 09:17:50 Full thread dump OpenJDK 64-Bit Server VM (20.0-b12 mixed mode): "Attach Listener" daemon prio=10 tid=0x00007f118c001000 nid=0x3091 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "DestroyJavaVM" prio=10 tid=0x00007f11cc010800 nid=0x3021 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE ...(ä¸ç¥)... Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x00007f11940036c8 (object 0x00000007ac7596f8, a java.lang.Object), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x00007f1194005e28 (object 0x00000007ac759708, a java.lang.Object), which is held by "Thread-1" Java stack information for the threads listed above: =================================================== "Thread-1": at Thread2.run(DeadLockTest.java:32) - waiting to lock <0x00000007ac7596f8> (a java.lang.Object) - locked <0x00000007ac759708> (a java.lang.Object) at java.lang.Thread.run(Thread.java:679) "Thread-0": at Thread1.run(DeadLockTest.java:19) - waiting to lock <0x00000007ac759708> (a java.lang.Object) - locked <0x00000007ac7596f8> (a java.lang.Object) at java.lang.Thread.run(Thread.java:679) Found 1 deadlock.
çµæä¸é¨ã«è¦ãã¨ããã®ããã»ã¹ããããããã¯ãèµ·ããã¦ãããã¨ãæããã ã親åã«ããããããã¯ãæ¤åºãã¦åºåãã¦ããã¦ããã
ããã§ã¯ãä»®ã«ãã® "Found one Java-level deadlock" ã¨ããæ
å ±ãç¡ããã®ã¨ãã¦ãã©ã®ããã«ã¹ã¬ãããã³ããèªããããã®èªã¿æ¹ã«ã¤ãã¦è¨è¿°ãããã¨ã«ããã
ã¹ã¬ãããã³ãã¯ãåºæ¬çã«ä¸è¨ã®ãããªãã©ã¼ãããã®ç¹°ãè¿ãã§åºåãããã
ã¹ã¬ããå prio=åªå 度 tid=ã¹ã¬ããID nid=管çID ç¶æ ã¹ã¿ãã¯ã¢ãã¬ã¹ java.lang.Thread.State: ç¶æ ã¹ã¿ãã¯ãã¬ã¼ã¹1 ã¹ã¿ãã¯ãã¬ã¼ã¹2 ...
åºåããã¹ã¬ãããã³ããè¦ãã¨ã¬ãã¼ã¸ã³ã¬ã¯ãç¨ã®ã¹ã¬ãããªã©å¤æ°åå¨ãããã注ç®ãã¹ã㯠Thread-1, Thread-2 ã®ãã°ã ã
"Thread-1" prio=10 tid=0x00007f11cc18c000 nid=0x303b waiting for monitor entry [0x00007f11bbbfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread2.run(DeadLockTest.java:32)
- waiting to lock <0x00000007ac7596f8> (a java.lang.Object)
- locked <0x00000007ac759708> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)"Thread-0" prio=10 tid=0x00007f11cc17a000 nid=0x303a waiting for monitor entry [0x00007f11bbcfb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread1.run(DeadLockTest.java:19)
- waiting to lock <0x00000007ac759708> (a java.lang.Object)
- locked <0x00000007ac7596f8> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)
注ç®ãã¹ãã¯ã- locked
"Thread-1" prio=10 tid=0x00007f11cc18c000 nid=0x303b waiting for monitor entry [0x00007f11bbbfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread2.run(DeadLockTest.java:32)
- waiting to lock <0x00000007ac7596f8> (a java.lang.Object)
- locked <0x00000007ac759708> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)
"Thread-0" prio=10 tid=0x00007f11cc17a000 nid=0x303a waiting for monitor entry [0x00007f11bbcfb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread1.run(DeadLockTest.java:19)
- waiting to lock <0x00000007ac759708> (a java.lang.Object)
- locked <0x00000007ac7596f8> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)
次ã«ã"Thread-0" ã® waiting to lock ã«æ³¨ç®ããã Thread-0 ã§ã¯ <0x00000007ac759708> ã¨ããã¢ãã¬ã¹ã®ãªãã¸ã§ã¯ãã®ããã¯ãç²å¾ãããã¨ãã¦ãããç²å¾ã§ããã«å¾ ã¡ç¶æ ã«ãªã£ã¦ãããå ç¨ã¨åæ§ã«ãã®ã¢ãã¬ã¹ã«æ³¨ç®ããã¨ã"Thread-1" ã§ããã¯ãã¦ãããã¨ããããã
ã¤ã¾ããäºãã®ããã¯è§£æ¾ãå¾ ã£ã¦ããç¶æ ã§ããããããã¯ã«ãªã£ã¦ãããã¨ãããã£ãã
"Thread-1" prio=10 tid=0x00007f11cc18c000 nid=0x303b waiting for monitor entry [0x00007f11bbbfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread2.run(DeadLockTest.java:32)
- waiting to lock <0x00000007ac7596f8> (a java.lang.Object)
- locked <0x00000007ac759708> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)"Thread-0" prio=10 tid=0x00007f11cc17a000 nid=0x303a waiting for monitor entry [0x00007f11bbcfb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread1.run(DeadLockTest.java:19)
- waiting to lock <0x00000007ac759708> (a java.lang.Object)
- locked <0x00000007ac7596f8> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)
ãã® waiting to lock 㨠locked ãçºçãã¦ããã®ã¯ DeadLockTest ã® 32 è¡ç®ã¨ 19 è¡ç®ã§ãããã¨ãèªã¿åããã
"Thread-1" prio=10 tid=0x00007f11cc18c000 nid=0x303b waiting for monitor entry [0x00007f11bbbfa000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread2.run(DeadLockTest.java:32)
- waiting to lock <0x00000007ac7596f8> (a java.lang.Object)
- locked <0x00000007ac759708> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)"Thread-0" prio=10 tid=0x00007f11cc17a000 nid=0x303a waiting for monitor entry [0x00007f11bbcfb000]
java.lang.Thread.State: BLOCKED (on object monitor)
at Thread1.run(DeadLockTest.java:19)
- waiting to lock <0x00000007ac759708> (a java.lang.Object)
- locked <0x00000007ac7596f8> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:679)
ã§ã¯æ¹ã㦠DeadLockTest ã®ã½ã¼ã¹ãèªããä¸è¨ã®å¤ªåã®è¡ã 19 è¡ç®ã¨ 32 è¡ç®ãã§ã¯ãªãããªããä¸è¡ããã¦19è¡ç®ã¨32è¡ç®ã¯System.out.printlnã®è¡ã ã£ã(ãªãããã¦ããã®ã ããï¼)ããã ããã¾ã§éå®ã§ããã°ããã¯ãã¦ãç®æã¯æãããªã®ã§ã18è¡ç®ã¨31è¡ç®ã® synchronized ã®è¡ã太åã«ãã¦ããã
...
// resource1, resource2 ã®é ã«ããã¯ãã
class Thread1 implements Runnable {
@Override
public void run() {
synchronized (DeadLockTest.resource1) {
System.out.println("thread1 - locked resource1");
synchronized (DeadLockTest.resource2) {
System.out.println("thread1 - locked resource2");
}
}
}
}// resource2, resource1 ã®é ã«ããã¯ãã
class Thread2 implements Runnable {
@Override
public void run() {
synchronized (DeadLockTest.resource2) {
System.out.println("thread2 - locked resource2");
synchronized (DeadLockTest.resource1) {
System.out.println("thread2 - locked resource1");
}
}
}
}
ããã¯ãããããã¯ç¨ã«ä½ã£ãã³ã¼ããªã®ã§ã©ãããããããã¯ãèµ·ããã¦ãããã¯èªæã ããå®éã¯ããå°ãè¤éã ãéè¦ãªã®ã¯ãjstack ã§åå¾åºæ¥ãã¹ã¬ãããã³ãã§ããã¾ã§ç°¡åã«ãããã¦ã¹ãã¼ãã£ã«åå ã¨æãããç®æãç¹å®åºæ¥ãç¹ã ãã¾ãããããããã¯ã¯ãã¾ãèµ·ãããã®ã§ã¯ãªãã®ã§ã¹ã¬ãããã³ããåå¾ãã¦ããããããã¯ã®æ¢ç´¢ã«ã¯ãã¾ã使ããªãããæ§è½åé¡ã調ã¹ãå ´åã«ã¯ã¹ã¬ãããã³ãã¯é常ã«æ´»èºãããããã¯ãç²å¾ããã®ã«å¤éã®ã¹ã¬ãããå¾ ã£ã¦ãããããªå ´åã¯ä¸ç®çç¶ã ããã®ãããªå ´åãThread.State ã WAITING ã«ãªã£ã¦ããã®ã§ Thread.State 㧠grep ããã°å³åº§ã«æããã«ãªãã ããã
Thread.State ãåãããç¶æ ã¯ä¸è¨ã®éãã
ã¹ã¬ããã®ç¶æ ã§ããã¹ã¬ããã®ç¶æ ã¯ã次ã®ã©ããã§ãã
http://java.sun.com/javase/ja/6/docs/ja/api/java/lang/Thread.State.html
- NEW
èµ·åãã¦ããªãã¹ã¬ããã®ç¶æ
RUNNABLE
Java ä»®æ³ãã·ã³ã§å®è¡ããã¦ããã¹ã¬ããã®ç¶æ - BLOCKED
ãããã¯ãããã¢ãã¿ã¼ããã¯ãå¾ æ©ãã¦ããã¹ã¬ããã®ç¶æ - WAITING
ã»ãã®ã¹ã¬ãããç¹å®ã®ã¢ã¯ã·ã§ã³ãå®è¡ããã®ãç¡æéã«å¾ æ©ãã¦ããã¹ã¬ããã®ç¶æ - TIMED_WAITING
æå®ãããå¾ æ©æéãã»ãã®ã¹ã¬ãããã¢ã¯ã·ã§ã³ãå®è¡ããã®ãå¾ æ©ãã¦ããã¹ã¬ããã®ç¶æ - TERMINATED
çµäºããã¹ã¬ããã®ç¶æ
ã¾ã¨ããã¨ãã¹ã¬ãããã³ããåã£ã¦èªããããã«ãªããã¨ã¯æ§è½åé¡ã®è§£æ±ºã«é常ã«å½¹ç«ã¤ãã¼ã«ã»è½åã¨ãªãã
ï¼ã¤ä¸ã®ããã°ã©ãã«ãªããããç¿å¾ãã¦ããã¹ãã
å¢è£æ¹è¨ç Javaè¨èªã§å¦ã¶ãã¶ã¤ã³ãã¿ã¼ã³å ¥é ãã«ãã¹ã¬ããç·¨
- ä½è : çµå浩
- åºç社/ã¡ã¼ã«ã¼: SBã¯ãªã¨ã¤ãã£ãæ ªå¼ä¼ç¤¾
- çºå£²æ¥: 2014/02/14
- ã¡ãã£ã¢: Kindleç
- ãã®ååãå«ãããã° (1件) ãè¦ã