java - 为什么有不同数量的线程?

标签 java multithreading scala java-threads

我的 scala 程序的一部分:

import java.util.concurrent.{ExecutorService, Executors, TimeUnit}

val genericExecutorService = Executors.newCachedThreadPool()

val scheduledExecutorService = Executors.newScheduledThreadPool(4)

val scheduledExecutorService1 = Executors.newScheduledThreadPool(1)

val scheduledExecutorService2 = Executors.newScheduledThreadPool(1)

我从日志文件中统计了线程数,数量为120。日志文件部分:

2018-10-18 00:00:00,421 INFO  [pool-7-thread-32] xxx

对日志文件中的线程进行计数:

$ grep -r "thread" xxx.log | grep -Po '(?<=(\[)).*(?=\])' | sort | uniq -c | wc -l
120

我统计了java的线程数

$ cat /proc/2966/status | grep Threads
Threads:    1524
$ ps -eLF| grep -c java
1525
$ ps -L -o pid= -p 2966 | wc -l
1524

为什么1525120如此不同?欢迎任何提示。 谢谢。

最佳答案

您可以使用更好的工具来分析 JVM 应用程序中的线程,而不是依赖日志记录。有一些可视化工具,例如 jmcjconsole。有jstack如果您使用的 PROD 机器可能无法使用可视化工具。

如果我关注 JVM 应用程序,

object Testing {

  def main(args: Array[String]): Unit = {

    import java.util.concurrent.{ExecutorService, Executors, TimeUnit}

    val genericExecutorService = Executors.newCachedThreadPool()

    val scheduledExecutorService = Executors.newScheduledThreadPool(4)

    val scheduledExecutorService1 = Executors.newScheduledThreadPool(1)

    val scheduledExecutorService2 = Executors.newScheduledThreadPool(1)

    genericExecutorService.execute(() => {
      println("0")
    })

    scheduledExecutorService.execute(() => {
      println("1")
    })

    scheduledExecutorService1.execute(() => {
      println("2")
    })

    scheduledExecutorService2.execute(() => {
      println("3")
    })
  }

}

我可以使用该应用程序的 processId 使用 jstack 获取线程,

$ jstack 68209 | grep thread
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.151-b12 mixed mode):
"pool-4-thread-1" #14 prio=5 os_prio=31 tid=0x00007ff1b812a800 nid=0xa503 waiting on condition [0x000070000c305000]
"pool-3-thread-1" #13 prio=5 os_prio=31 tid=0x00007ff1b812a000 nid=0xa703 waiting on condition [0x000070000c202000]
"pool-2-thread-1" #12 prio=5 os_prio=31 tid=0x00007ff1b80f9000 nid=0xa903 waiting on condition [0x000070000c0ff000]
"pool-1-thread-1" #11 prio=5 os_prio=31 tid=0x00007ff1b9869800 nid=0x5803 waiting on condition [0x000070000bffc000]
"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007ff1b9802800 nid=0x1c07 runnable 
"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007ff1b9001000 nid=0x1d03 runnable 
"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007ff1ba804800 nid=0x2b03 runnable 
"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007ff1ba805000 nid=0x5303 runnable 
"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007ff1ba805800 nid=0x5103 runnable 
"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007ff1b8810800 nid=0x2c03 runnable 
"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007ff1b981e800 nid=0x4e03 runnable 
"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007ff1b981f000 nid=0x4c03 runnable 

您也可以简单地执行jstack pid,但这会提供线程的完整转储,包括 thread_state 和所有内容。

使用jmc可视化工具,

jmc

可能有帮助: Monitoring queue of ExecutionContextExecutor “scala.concurrent.ExecutionContext.Implicits.global”

关于java - 为什么有不同数量的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883277/

相关文章:

scala - 如何组合两个具有zio效果和不同类型环境的Http4s路由

Android Textview 中的 Java 字符串排序

Java String.split(regex) 不工作

multithreading - Action Script 3.0中的异步

java - 在 Scala 中,我可以对参数和类字段使用相同的名称吗

scala - 获取可被 1 - 1000 中的 3 或 5 整除的元素列表

java - 在 second/之前删除字符串的一部分

java - Maven 无法在 Mac OS X 中的 Tomcat 服务器上部署 Web 应用程序

java - 在 Java 中使用线程的并行素数检查器

java - 是否没有办法迭代或复制 Java ThreadLocal 的所有值?