java - java线程转储中的高线程数

标签 java wildfly cpu-usage

我在 wildfly-9.0.1.Final 上运行的 java web 应用程序有时会消耗非常高的内存和 CPU。 这次我得到了线程转储,发现超过 7K 个线程具有相同的线程,但堆栈跟踪如下。

pool-47940-thread-1 - priority:5 - threadId:0x000000029ad3f800 - nativeId:0xfad0 - state:WAITING
stackTrace:
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007a7d1fbe0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronize

我使用 this 分析线程转储

这是相同的屏幕截图。 enter image description here

enter image description here

我在我的应用程序中多次使用下面的代码

ExecutorService executor = Executors.newFixedThreadPool(3);
//business logic
// This will make the executor accept no new threads and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {

}

ablow 代码是否会导致任何问题?

这个线程是内存和 CPU 使用率高的根本原因吗?

最佳答案

在我上面的代码中,我使用了 while (!executor.isTerminated()){} 来等待所有任务完成他们的工作。此 while 循环使用 CPU 直到所有任务完成,这可能会导致高 CPU 问题。

我修改了我的代码如下。这解决了我的高 CPU 问题。

ExecutorService executor = Executors.newFixedThreadPool(3);
executor.shutdown();
executor.invokeAll(listOfTask);  // invokAll method will wait untill all task finished, No while loop here

更多信息read this

关于java - java线程转储中的高线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398942/

相关文章:

java - SQL : WHERE not working with JTable

java - 如何在完整项目中以 Debug模式跟踪 System.out.println

java - WFLYEE0040 : A component named '...' is already defined in this module

mysql - Wildfly MySQL 数据源 : service jboss. jdbc-driver.mysql (missing) dependents

java - jpa,尝试从实体创建表,找不到类 ExpressionVisitor

java - 检测java web应用程序中cpu使用率15%的原因

java - 多个并发SQL查询-性能查询

Java在从另一个文件读取时创建 double 组

windows - Android 模拟器 (qemu-system-i386.exe) 的 CPU 使用率过高

r - 为什么我的 R 在运行诸如 step() 之类的函数时使用所有 CPU 内核?