运行服务器,然后按[ctrl] [/],会运行一个系统线程堆,这样将输出当前运行的所有线程,例如:
^Full thread dump:
Thread-1" prio=5 tid=0x87fe0 nid=0x800013b8 waiting on monitor [0..0xbfffe9f8]
"Thread-0" prio=5 tid=0xafde0 nid=0x72a00 runnable [0x173f000..0x173fb48]
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:421)
at java.net.ServerSocket.implAccept(ServerSocket.java:243)
at java.net.ServerSocket.accept(ServerSocket.java:222)
at com.generationjava.net.PortListener.run(PortListener.java:38)
at java.lang.Thread.run(Thread.java:496)
"Signal Dispatcher" daemon prio=10 tid=0xa5f80 nid=0x52af0 waiting on monitor [0..0]
"Finalizer" daemon prio=8 tid=0xa46c0 nid=0x4d930 waiting on monitor [0x10a8000..0x10a8b48]
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:108)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:123)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:162)
"Reference Handler" daemon prio=10 tid=0xa3b70 nid=0x4cca0 waiting on monitor [0x1027000..0x1027b48]
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:420)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:110)
"VM Thread" prio=5 tid=0xa2de0 nid=0x4ca20 runnable
"VM Periodic Task Thread" prio=10 tid=0xa56b0 nid=0x52890 waiting on monitor
后面的四个线程是标准线程,而其他的则跟运行的服务器程序有关。这两个线程被命名为Thread-1和Thread-0,如果用Thread th = new Thread(runnable,"Server")代替Thread th = new Thread(runnable)去创建进程,你就可以命名自己要创建的线程。
另外,如果你调用java.lang.Thread的setName(String)方法,你也能命名你的线程,并且可以很容易从线程堆的输出中发现原来的Thread-0是服务器程序。
"Server" prio=5 tid=0xafde0 nid=0x72a00 runnable [0x173f000..0x173fb48]
而原来的Thread-1线程现在变成了Thread-0。
通过命名线程和取得线程堆,开发者就能看到Java虚拟机(JVM)的内部状态。如果想得到更多信息的话,你就得去研究Java Debugger了。
转自: www.ZDnet.com.cn Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
标签: 线程