通过jstack分析解决进程死锁问题实例代码

刚才用jstack解决了一个进程死锁的问题——其实早就解决了,也知道原因,只是一直没找到死锁的位置,不太甘心而已。

流程大致如下:

(0)环境要求,JDK1.6及以上

(1)先找到进程的PID,Windows下,打开进程管理器,按照名字排序,可以找到叫做javaw.exe的进程(java虚拟机进程一律叫做javaw.exe),要找出哪个是你的进程,记住当前进程列表,然后重启你的进程,PID刷新过的那个即是你的进程。

(2)在CMD下运行:jstack pid,jstack会在console上打出一系列信息

(3)分析上述信息

实例:

我这个问题的的jstack信息如下:

C:\Documents and Settings\user>jstack 6652
2012-06-07 21:32:02
Full thread dump Java HotSpot(TM) Client VM (16.3-b01 mixed mode, sharing):

"Thread-1" daemon prio=6 tid=0x03010c00 nid=0xcdc waiting for monitor entry [0x0
339f000]
  java.lang.Thread.State: BLOCKED (on object monitor)
    at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStr
eam.java:122)
    - waiting to lock <0x22942280> (a org.apache.commons.net.ftp.FTPClient)
    at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream
.java:535)
    at java.lang.Thread.run(Thread.java:619)

"Framework Event Dispatcher" daemon prio=6 tid=0x03010400 nid=0x998 in Object.wa
it() [0x0334f000]
  java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x228fa800> (a org.eclipse.osgi.framework.eventmgr.EventMa
nager$EventThread)
    at java.lang.Object.wait(Object.java:485)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.getNextE
vent(EventManager.java:400)
    - locked <0x228fa800> (a org.eclipse.osgi.framework.eventmgr.EventManage
r$EventThread)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(Even
tManager.java:336)

"Start Level Event Dispatcher" daemon prio=6 tid=0x02fcf400 nid=0x2638 in Object
.wait() [0x032de000]
  java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x2295db48> (a [I)
    at java.lang.Object.wait(Object.java:485)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStrea
m.java:339)
    - locked <0x2295db48> (a [I)
    at org.apache.commons.net.telnet.TelnetInputStream.read(TelnetInputStrea
m.java:466)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    - locked <0x2295fe18> (a java.io.BufferedInputStream)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    - locked <0x22961f88> (a java.io.InputStreamReader)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    - locked <0x22961f88> (a java.io.InputStreamReader)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:264)
    at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:335)
    at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:5
50)
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:163)
    at com.mycompany.dc.ftp.client.FTPClientImpl.connect(FTPClientImpl.java:7
5)
    - locked <0x22942280> (a org.apache.commons.net.ftp.FTPClient)
    at com.mycompany.dc.ftp.client.FTPClientFactoryImpl.getClient(FTPClientFa
ctoryImpl.java:35)
    - locked <0x228f9310> (a java.lang.Object)
    at ftpclienttest.Activator.start(Activator.java:43)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(Bund
leContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActiv
ator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(Bund
leContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(Bundl
eHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(Abstra
ctBundle.java:389)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Frame
work.java:1131)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBund
les(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBund
les(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(St
artLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStart
Level(StartLevelManager.java:243)
    - locked <0x27e68d70> (a java.lang.Object)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEv
ent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEv
ent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventM
anager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(Even
tManager.java:340)

"Framework Active Thread" prio=6 tid=0x02ff1800 nid=0x1fbc in Object.wait() [0x0
328f000]
  java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x27e65770> (a org.eclipse.osgi.framework.internal.core.Fr
amework)
    at org.eclipse.osgi.framework.internal.core.Framework.run(Framework.java
:1817)
    - locked <0x27e65770> (a org.eclipse.osgi.framework.internal.core.Framew
ork)
    at java.lang.Thread.run(Thread.java:619)

"OSGi Console" prio=6 tid=0x03005400 nid=0x225c waiting on condition [0x0323f000
]
  java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at org.eclipse.osgi.framework.internal.core.FrameworkConsole.runConsole(
FrameworkConsole.java:125)
    at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(Framewo
rkConsole.java:104)
    at java.lang.Thread.run(Thread.java:619)

"Low Memory Detector" daemon prio=6 tid=0x02c09800 nid=0x1d68 runnable [0x000000
00]
  java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x02c03000 nid=0x24c4 waiting on condition
[0x00000000]
  java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x02c01800 nid=0x1138 waiting on condition
[0x00000000]
  java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x02c20c00 nid=0x18ac runnable [0x0000000
0]
  java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x02bc0400 nid=0x11ac in Object.wait() [0x02d8f000
]
  java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x27d5e5c8> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
    - locked <0x27d5e5c8> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=0x02bbb800 nid=0x9cc in Object.wait() [0x
02d3f000]
  java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x27d5e650> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
    - locked <0x27d5e650> (a java.lang.ref.Reference$Lock)

"main" prio=6 tid=0x008a6c00 nid=0x22ec in Object.wait() [0x0098f000]
  java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x22c303c8> (a org.eclipse.core.runtime.internal.adaptor.S
emaphore)
    at org.eclipse.core.runtime.internal.adaptor.Semaphore.acquire(Semaphore
.java:55)
    - locked <0x22c303c8> (a org.eclipse.core.runtime.internal.adaptor.Semap
hore)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.updateSplash(EclipseS
tarter.java:1251)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.setStartLevel(Eclipse
Starter.java:1213)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarte
r.java:288)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.ja
va:175)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

"VM Thread" prio=10 tid=0x02bba000 nid=0xdb4 runnable

"VM Periodic Task Thread" prio=10 tid=0x02c0e400 nid=0x24ac waiting on condition

JNI global references: 677

分析:

根据提示,有两个线程都用到了ftpClient这个对象作为锁,而且前一个得到锁的要等待后一个需要这个锁的返回结果,造成死锁。这两处分别为:

(1) at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStr

eam.java:122)

(2) - locked <0x22942280> (a org.apache.commons.net.ftp.FTPClient)

at com.sagemcom.dc.ftp.client.FTPClientFactoryImpl.getClient(FTPClientFa

ctoryImpl.java:35)

- locked <0x228f9310> (a java.lang.Object)

前者是系统自己用的锁,后者是我代码里面加的。自己代码里面换一个对象做锁就解决了。

总结

jstack在解决问题上还是比较有帮助的,信息简洁有效,其实有很多图形化的分析工具是基于它的。但jstack需要jdk1.6以上的版本才支持。

以上就是本文关于通过jstack分析解决进程死锁问题实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。