如何让java线程在不同的cpu中运行?

时间:2016-05-18 16:30:19

标签: java multithreading process cpu

几乎所有线程都只在一个Linux进程下运行。 如何修改我的应用程序以使其成为多CPU。 当我使用jstack命令查看进程堆栈时,我可以看到 大部分工作都是通过这个过程来完成的

sudo -u orca jstack 15370
2016-05-17 13:50:45
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.65-b04 mixed mode):

"SET:4" daemon prio=10 tid=0x0000000000b5a000 nid=0x3c70 waiting on condition [0x00007fcf206af000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"SET:3" daemon prio=10 tid=0x0000000000b58800 nid=0x3c6f waiting on condition [0x00007fcf207b0000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"SET:2" daemon prio=10 tid=0x0000000000b57000 nid=0x3c6e waiting on condition [0x00007fcf208b1000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"SET:1" daemon prio=10 tid=0x0000000000b55800 nid=0x3c6d waiting on condition [0x00007fcf209b2000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.ConnSet$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"InconmingConnMgr" daemon prio=10 tid=0x0000000000a81800 nid=0x3c6c runnable [0x00007fcf20ab3000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x00000006000383a0> (a sun.nio.ch.Util$2)
    - locked <0x0000000600038390> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000060000fe68> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"processRequestForActualGoSafe" daemon prio=10 tid=0x0000000000a7e800 nid=0x3c69 waiting on condition [0x00007fcf20bb4000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ManagerBase$3.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"collectConnStatsGoSafe" daemon prio=10 tid=0x0000000000a7c000 nid=0x3c68 waiting on condition [0x00007fcf20cb5000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ManagerBase.collectStatistics(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ManagerBase.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ManagerBase$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"InconmingRequestMgr" daemon prio=10 tid=0x0000000000b0c800 nid=0x3c67 runnable [0x00007fcf20db6000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x00000006000661f8> (a sun.nio.ch.Util$2)
    - locked <0x00000006000661e8> (a java.util.Collections$UnmodifiableSet)
    - locked <0x0000000600066208> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at com.orca.pf.tc50.rt_manager.phprequest.PHPSocket.processRequests(Unknown Source)
    at com.orca.pf.tc50.rt_manager.phprequest.PHPSocket.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.phprequest.PHPSocket$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"InconmingConnMgr" daemon prio=10 tid=0x0000000000b08800 nid=0x3c66 runnable [0x00007fcf20eb7000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x0000000600066340> (a sun.nio.ch.Util$2)
    - locked <0x0000000600066330> (a java.util.Collections$UnmodifiableSet)
    - locked <0x00000006000355e0> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase.processChannels(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.connection_manager.VehicularConnectionManagerBase$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"CypressFrameGenerator" daemon prio=10 tid=0x0000000000a91800 nid=0x3c65 in Object.wait() [0x00007fcf20fb8000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000060007a790> (a com.orca.pf.tc50.rt_manager.cypress.CypressFrameGenerator)
    at java.lang.Object.wait(Object.java:503)
    at com.orca.pf.tc50.rt_manager.cypress.CypressFrameGenerator.backgroundProcess(Unknown Source)
    - locked <0x000000060007a790> (a com.orca.pf.tc50.rt_manager.cypress.CypressFrameGenerator)
    at com.orca.pf.tc50.rt_manager.cypress.CypressFrameGenerator.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.cypress.CypressFrameGenerator$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"VqXmlGenerator" daemon prio=10 tid=0x0000000000a94800 nid=0x3c64 in Object.wait() [0x00007fcf210b9000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000006000663e8> (a com.orca.pf.tc50.rt_manager.VQuebec.XmlGeneratorVQuebec)
    at java.lang.Object.wait(Object.java:503)
    at com.orca.pf.tc50.rt_manager.VQuebec.XmlGeneratorVQuebec.backgroundProcess(Unknown Source)
    - locked <0x00000006000663e8> (a com.orca.pf.tc50.rt_manager.VQuebec.XmlGeneratorVQuebec)
    at com.orca.pf.tc50.rt_manager.VQuebec.XmlGeneratorVQuebec.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.VQuebec.XmlGeneratorVQuebec$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"MtqXmlGenerator" daemon prio=10 tid=0x0000000000b17000 nid=0x3c63 in Object.wait() [0x00007fcf211ba000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000600066488> (a com.orca.pf.tc50.rt_manager.mtqxml.XmlGeneratorMTQ)
    at java.lang.Object.wait(Object.java:503)
    at com.orca.pf.tc50.rt_manager.mtqxml.XmlGeneratorMTQ.backgroundProcess(Unknown Source)
    - locked <0x0000000600066488> (a com.orca.pf.tc50.rt_manager.mtqxml.XmlGeneratorMTQ)
    at com.orca.pf.tc50.rt_manager.mtqxml.XmlGeneratorMTQ.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.mtqxml.XmlGeneratorMTQ$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"TimeZoneCheck" daemon prio=10 tid=0x0000000000b14800 nid=0x3c62 waiting on condition [0x00007fcf212bb000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.timezone.TimeZoneChangedManager.checkProc(Unknown Source)
    at com.orca.pf.tc50.rt_manager.timezone.TimeZoneChangedManager.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.timezone.TimeZoneChangedManager$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"processNewPingOptionActivation" daemon prio=10 tid=0x0000000000b13000 nid=0x3c61 in Object.wait() [0x00007fcf213bc000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000600066c70> (a com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager)
    at java.lang.Object.wait(Object.java:503)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.realTimeBackground(Unknown Source)
    - locked <0x0000000600066c70> (a com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"pingOptionsNightlyProcess" daemon prio=10 tid=0x0000000000a9b000 nid=0x3c60 waiting on condition [0x00007fcf214bd000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.backgroundProcess(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"processTripStats" daemon prio=10 tid=0x0000000000aa0000 nid=0x3c5b in Object.wait() [0x00007fcf215be000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000006000670d8> (a com.orca.pf.tc50.rt_manager.stats.ReportStatistics)
    at java.lang.Object.wait(Object.java:503)
    at com.orca.pf.tc50.rt_manager.stats.ReportStatistics.processStatsThread(Unknown Source)
    - locked <0x00000006000670d8> (a com.orca.pf.tc50.rt_manager.stats.ReportStatistics)
    at com.orca.pf.tc50.rt_manager.stats.ReportStatistics.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.stats.ReportStatistics$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"processRequestForActualTC50" daemon prio=10 tid=0x0000000000a2c800 nid=0x3c5a waiting on condition [0x00007fcf216bf000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ManagerBase$3.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"collectConnStatsTC50" daemon prio=10 tid=0x0000000000a2b800 nid=0x3c59 waiting on condition [0x00007fcf217c0000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ManagerBase.collectStatistics(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ManagerBase.access$100(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ManagerBase$2.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"DB connection reaper" daemon prio=10 tid=0x00000000009fb000 nid=0x3c54 waiting on condition [0x00007fcf218c1000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.databasepool.DatabaseConnectionPool.run(Unknown Source)

"DB connection reaper" daemon prio=10 tid=0x00000000009c0800 nid=0x3c42 waiting on condition [0x00007fcf219c2000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.databasepool.DatabaseConnectionPool.run(Unknown Source)

"Log Sync" daemon prio=10 tid=0x000000000094c000 nid=0x3c3f waiting on condition [0x00007fcf21ad9000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.app.logger.LoggerFile.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

"Service Thread" daemon prio=10 tid=0x00000000007dc800 nid=0x3c3c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x00000000007da800 nid=0x3c3b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x00000000007d7800 nid=0x3c3a waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x00000000007cf000 nid=0x3c39 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x00000000007b4800 nid=0x3c2b in Object.wait() [0x00007fcf2318d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000600021248> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0x0000000600021248> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" daemon prio=10 tid=0x00000000007b2800 nid=0x3c2a in Object.wait() [0x00007fcf2328e000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000600020c58> (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 <0x0000000600020c58> (a java.lang.ref.Reference$Lock)

"Main/ConsoleGoSafe" prio=10 tid=0x0000000000746000 nid=0x3c0a runnable [0x00007ffdefc84000]
   java.lang.Thread.State: RUNNABLE
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
    at java.net.ServerSocket.implAccept(ServerSocket.java:530)
    at java.net.ServerSocket.accept(ServerSocket.java:498)
    at com.orca.pf.tc50.console.EmbeddedConsole.run(Unknown Source)
    at com.orca.pf.tc50.rt_manager.console.ManagerConsole.run(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ManagerTC50.main(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at MDLoader.main(MDLoader.java:24)

"VM Thread" prio=10 tid=0x00000000007ae800 nid=0x3c29 runnable

"GC task thread#0 (ParallelGC)" prio=10 tid=0x000000000075c000 nid=0x3c16 runnable

"GC task thread#1 (ParallelGC)" prio=10 tid=0x000000000075d800 nid=0x3c17 runnable

"GC task thread#2 (ParallelGC)" prio=10 tid=0x000000000075f800 nid=0x3c18 runnable

"GC task thread#3 (ParallelGC)" prio=10 tid=0x0000000000761800 nid=0x3c19 runnable

"GC task thread#4 (ParallelGC)" prio=10 tid=0x0000000000763000 nid=0x3c1a runnable

"GC task thread#5 (ParallelGC)" prio=10 tid=0x0000000000765000 nid=0x3c1b runnable

"GC task thread#6 (ParallelGC)" prio=10 tid=0x0000000000767000 nid=0x3c1c runnable

"GC task thread#7 (ParallelGC)" prio=10 tid=0x0000000000769000 nid=0x3c1d runnable

"VM Periodic Task Thread" prio=10 tid=0x00000000007ef800 nid=0x3c3d waiting on condition

JNI global references: 157

2 个答案:

答案 0 :(得分:1)

在线程和进程方面,你的想法似乎有些混乱。在Linux中实现线程的方式也可能使它更加混乱。

在Linux中,每个“进程”都有一个PID。每个轻量级进程也有一个PID。您在 Java的线程转储中认识到的线程是Linux上的一个轻量级进程。主JVM进程创建的所有线程共享相同的地址空间。

但单独查看线程转储并不能告诉您哪个线程在哪个CPU(处理器)上运行。实际上,基于Linux中的调度算法,可以(并且可能将)将相同的线程分配给各种CPU。最好的情况是,您可以使用其他Linux工具查看有多少处理器(例如lshwpstop)以及它们正在做什么。

这通过一些十六进制和十进制标识进一步复杂化。在线程转储中考虑这个线程:

"pingOptionsNightlyProcess" daemon prio=10 tid=0x0000000000a9b000 
nid=0x3c60 waiting on condition [0x00007fcf214bd000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.backgroundProcess(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager.access$000(Unknown Source)
    at com.orca.pf.tc50.rt_manager.ping_options.PingOptionsManager$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

此主题的本机线程ID为0x3c60,与十进制的15456相同。如果您在jstack上调用15456而不是15370(就像您所做的那样),您将获得完全相同的线程转储!

将Java中的线程(由线程转储标识)与操作系统级别的线程相关联的强大工具是顶部及其threads mode。我在Top and Java Threads撰写了大量有关此内容的文章。我也可以在这里重现它,但有一些描述。

您的应用程序似乎已经创建了许多线程。因此,为了让您的程序在具有多个处理器(内核)的计算机上利用多个CPU,您不需要做任何事情(除了确保您没有违反线程安全性)。

答案 1 :(得分:0)

您可以使用java.util.concurrent API来利用多个CPU核心。

  1. 使用Executors newFixedThreadPool API

    public static ExecutorService newFixedThreadPool(int nThreads,
                                   ThreadFactory threadFactory)
    

    用Runtime.getRuntime()替换nThreads.availableProcessors()

  2. 使用newWorkStealingPool() API

    public static ExecutorService newWorkStealingPool()
    
      

    使用所有可用处理器作为目标并行级别创建工作窃取线程池。

  3. 创建ThreadPoolExecutor并在以下API中设置池大小

    public ThreadPoolExecutor(int corePoolSize,
                      int maximumPoolSize,
                      long keepAliveTime,
                      TimeUnit unit,
                      BlockingQueue<Runnable> workQueue)
    

    Runtime.getRuntime().availableProcessors()作为参数传递给池大小。