线程数从OS到OS不等

时间:2012-12-04 09:46:48

标签: java multithreading macos windows-7 daemon

当我们运行我们的守护程序应用程序时,它的线程数因不同的操作系统而异。 即,当代码在Windows 7中执行时,它需要15个线程(当代码运行1.5小时时),当在MAC OS X中执行相同的代码相同的时间段时它使用26个线程。 / p>

注意:两台机器都使用相同的程序代码。我怀疑的是,Windows是32位,Mac OS是64位,这是否会改变线程数?

什么都可能是线程数变化的原因?

对此有任何帮助将不胜感激..


感谢大家分享您的解决方法和解决方案。 我终于在Mac中接受了守护进程的线程转储,并且能够发现线程中存在一些锁。但是我仍然不知道为什么Mac OS中的线程数始终低于Windows 7线程数。 在下面找到带锁的线程转储。

"DefaultUDPTransportMapping_10.190.146.17/0" - Thread t@2254
   java.lang.Thread.State: RUNNABLE
                at java.net.PlainDatagramSocketImpl.receive0(Native Method)
                - locked java.net.PlainDatagramSocketImpl@2d82f79b
                at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
                - locked java.net.PlainDatagramSocketImpl@2d82f79b
                at java.net.DatagramSocket.receive(DatagramSocket.java:712)
                - locked java.net.DatagramPacket@35f2c1c9
                - locked java.net.DatagramSocket@54908eca
                at org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(DefaultUdpTransportMapping.java:345)
                at java.lang.Thread.run(Thread.java:637)


"RMI TCP Accept-0" - Thread t@12
   java.lang.Thread.State: RUNNABLE
                at java.net.PlainSocketImpl.socketAccept(Native Method)
                at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
                - locked java.net.SocksSocketImpl@26226ff2
                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)

1 个答案:

答案 0 :(得分: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线程都很好地命名,因此您可以找出他们是谁。