2012-12-04 45 views
1

當我們運行我們的守護程序應用程序時,它的線程數因不同的操作系統而異。 即,當在Windows 7中執行代碼時,它需要15個線程(當代碼運行1.5個小時)時,當相同的代碼在MAC OS X中執行相同的時間時,它將使用26個線程。線程數量因操作系統而異OS

注意:兩臺機器都使用相同的程序代碼。我的疑問是,Windows是32位,Mac OS是64位,這是否會改變線程數?

什麼都可能是這種線程數變化的原因?

上任何幫助將不勝感激..


謝謝大家分享你的解決方法和解決方案。 我終於把Mac中的守護程序的線程轉儲並且能夠發現線程中存在一些鎖。但是我仍然不知道爲什麼Mac OS中的線程數總是保持低於Windows 7線程數。 在下面找到帶鎖的線程轉儲。

"DefaultUDPTransportMapping_10.190.146.17/0" - Thread [email protected] 
    java.lang.Thread.State: RUNNABLE 
       at java.net.PlainDatagramSocketImpl.receive0(Native Method) 
       - locked [email protected] 
       at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136) 
       - locked [email protected] 
       at java.net.DatagramSocket.receive(DatagramSocket.java:712) 
       - locked [email protected] 
       - locked [email protected] 
       at org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(DefaultUdpTransportMapping.java:345) 
       at java.lang.Thread.run(Thread.java:637) 


"RMI TCP Accept-0" - Thread [email protected] 
    java.lang.Thread.State: RUNNABLE 
       at java.net.PlainSocketImpl.socketAccept(Native Method) 
       at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390) 
       - locked [email protected] 
       at java.net.ServerSocket.implAccept(ServerSocket.java:453) 
       at java.net.ServerSocket.accept(ServerSocket.java:421) 
       at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:34) 
       at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:369) 
       at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:341) 
       at java.lang.Thread.run(Thread.java:637) 
+0

向我們展示代碼。 – 2012-12-04 09:52:41

+0

你使用可能使用線程的框架嗎?也許本機代碼創建線程,這在不同的平臺上肯定不一樣。 – zeller

+1

我認爲由應用程序創建的實際線程數是相同的,但JVM使用不同數量的線程,具體取決於實現。 – assylias

回答

1

如果這些線程是由JVM進程創建的,你可以通過jstack得到他們的籌碼和名稱(例如)。你從它的輸出中得到的信息應該足以回答你的問題。比如我的機器用單線程應用jstack上給了我這樣的:

Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.5-b02 mixed mode): 

"Attach Listener" daemon prio=5 tid=0x00007f985a800000 nid=0x3e07 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE 

"Service Thread" daemon prio=5 tid=0x00007f9858871800 nid=0x4f03 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE 

"C2 CompilerThread1" daemon prio=5 tid=0x00007f9858871000 nid=0x4e03 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE 

"C2 CompilerThread0" daemon prio=5 tid=0x00007f985886f000 nid=0x4d03 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE 

"Signal Dispatcher" daemon prio=5 tid=0x00007f985886e000 nid=0x4c03 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE 

"Finalizer" daemon prio=5 tid=0x00007f9859813000 nid=0x3b03 in Object.wait() [0x00000001683c1000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) 
    - waiting on <0x0000000147a35798> (a java.lang.ref.ReferenceQueue$Lock)  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) 
    - locked <0x0000000147a35798> (a java.lang.ref.ReferenceQueue$Lock)  at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)  at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177) 

"Reference Handler" daemon prio=5 tid=0x00007f9859812000 nid=0x3a03 in Object.wait() [0x00000001682be000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) 
    - waiting on <0x0000000147a35320> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:503) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) 
    - locked <0x0000000147a35320> (a java.lang.ref.Reference$Lock) 

"main" prio=5 tid=0x00007f985880e000 nid=0x1107 waiting on condition [0x0000000108bd8000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at main(Example.java:24) 

"VM Thread" prio=5 tid=0x00007f985980f800 nid=0x3903 runnable 

"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007f985881c000 nid=0x3503 runnable 

"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007f985881c800 nid=0x3603 runnable 

"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007f985881d000 nid=0x3703 runnable 

"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007f985881e000 nid=0x3803 runnable 

"VM Periodic Task Thread" prio=5 tid=0x00007f9858857800 nid=0x5003 waiting on condition 

所有VM線程是很好的命名,所以你可以計算出他們是誰。