上面給出的答案是正確的。我只是回答完成圖片。顯然,JVM不會自動並行化用戶代碼,並且它自己的線程正在進行。這些線程數量有所不同,但通常我們有以下線程。線程堆棧的這個快照是在熱點(OpenJDK)JVM的活動階段開始時獲取的。我們可以在這裏看到11個線程,它們可以輕鬆佔用八核或四核機器的所有內核。線程名稱也解釋了它們的用途。
Full thread dump OpenJDK 64-Bit Server VM (25.71-b00-debug mixed mode):
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007ffff0160800 nid=0x2f91 runnable [0x0000000000000000]
"C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007ffff0158800 nid=0x2f90 waiting on condition [0x0000000000000000]
"C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007ffff0156000 nid=0x2f8f waiting on condition [0x0000000000000000]
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ffff0153000 nid=0x2f8e waiting on condition [0x0000000000000000]
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ffff0150800 nid=0x2f8d waiting on condition [0x0000000000000000]
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ffff014e800 nid=0x2f8c runnable [0x0000000000000000]
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ffff0103800 nid=0x2f8b in Object.wait() [0x00007ffff412f000]
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ffff00fa000 nid=0x2f8a in Object.wait() [0x00007ffff4230000]
"main" #1 prio=5 os_prio=0 tid=0x00007ffff000d000 nid=0x2f86 runnable [0x00007ffff7fca000]
"VM Thread" os_prio=0 tid=0x00007ffff00ef800 nid=0x2f89 runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007ffff015f000 nid=0x2f92 waiting on condition
類似地,線程堆棧的以下快照顯示在動作垃圾收集。這個快照是在GC調用後進行的。 GC有8個線程,因爲我正在運行GC的並行實現(我猜測GC線程等於核心數,因爲我正在測試八核機器)。
Full thread dump OpenJDK 64-Bit Server VM (25.71-b00-debug mixed mode):
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00007ffff017e800 nid=0xaa1 runnable (no locks) [0x0000000000000000]
"C1 CompilerThread3" #8 daemon prio=9 os_prio=0 tid=0x00007ffff016e800 nid=0xaa0 waiting on condition (no locks) [0x0000000000000000]
"C2 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007ffff016b800 nid=0xa9f waiting on condition (no locks) [0x0000000000000000]
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007ffff0169800 nid=0xa9e waiting on condition (no locks) [0x0000000000000000]
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007ffff0166000 nid=0xa9d waiting on condition (no locks) [0x0000000000000000]
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007ffff0164800 nid=0xa9c runnable (no locks) [0x0000000000000000]
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007ffff0119000 nid=0xa9b in Object.wait() (no locks) [0x00007fffba33d000]
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007ffff0110000 nid=0xa9a in Object.wait() (no locks) [0x00007fffba43e000]
"main" #1 prio=5 os_prio=0 tid=0x00007ffff000d000 nid=0xa8b runnable (no locks) [0x00007ffff7fc9000]
"VM Thread" os_prio=0 tid=0x00007ffff0105000 nid=0xa99 runnable (no locks)
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007ffff0026800 nid=0xa91 runnable (no locks)
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007ffff0028800 nid=0xa92 runnable (no locks)
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007ffff002a800 nid=0xa93 runnable (no locks)
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007ffff002c800 nid=0xa94 runnable (no locks)
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00007ffff002e800 nid=0xa95 runnable (no locks)
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00007ffff0030800 nid=0xa96 runnable (no locks)
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00007ffff0032800 nid=0xa97 runnable (no locks)
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00007ffff0034800 nid=0xa98 runnable (no locks)
"VM Periodic Task Thread" os_prio=0 tid=0x00007ffff017f800 nid=0xaa2 waiting on condition (no locks)
結論
由於線程的數目在運行時有所不同,許多線程啓動,並打死蒼蠅,你幾乎從來沒有看到一個Java程序使用單核心無論多久它運行。
上面的輸出是使用GDB和openJDK的內部調試API生成的。如果有人有興趣瞭解更多關於這些線程及其用途的信息,可以參考:Thread Management in openJDK
可能的重複[當前HotSpot JVM默認並行運行嗎?](http://stackoverflow.com/questions/ 12219424/does-the-current-hotspot-jvm-run-in-parallel-by-default) – Boann
GC是並行的,我的猜測是你正在使用所有的CPU創建如此多的垃圾。 –
如果是GC,那麼我爲什麼不用'-Xint'獲得相同的結果? – user11171