我的Scala程序的一部分:
public static void main(String[] args) {
setS(1);
setW(-3);
setA(78);
setG(23);
setY(-1005);
}
public static int getS() {
System.out.print('s');
return s;
}
public static void setS(int s) {
chapter7.s = s;
}
public static int getW() {
System.out.print('w');
return w;
}
public static void setW(int w) {
chapter7.w = w;
}
public static int getA() {
System.out.print(a);
return a;
}
public static void setA(int a) {
chapter7.a = a;
}
public static int getG() {
System.out.print(g);
return g;
}
public static void setG(int g) {
chapter7.g = g;
}
public static int getY() {
System.out.print(y);
return y;
}
public static void setY(int y) {
chapter7.y = y;
}
}
我计算了日志文件中的线程数,该线程数为120。部分日志文件:
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)
计数日志文件中的线程:
2018-10-18 00:00:00,421 INFO [pool-7-thread-32] xxx
我计算了Java的线程数
$ grep -r "thread" xxx.log | grep -Po '(?<=(\[)).*(?=\])' | sort | uniq -c | wc -l
120
为什么$ cat /proc/2966/status | grep Threads
Threads: 1524
$ ps -eLF| grep -c java
1525
$ ps -L -o pid= -p 2966 | wc -l
1524
和1525
如此不同?任何提示都欢迎。
谢谢。
答案 0 :(得分:1)
您可以使用更好的工具来分析JVM应用程序中的线程,而不必依赖日志记录。有视觉工具,例如jmc
和jconsole
。如果您使用PROD机器,则可能会有jstack
,而视觉工具可能不可用。
如果我正在关注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")
})
}
}
我可以使用该应用程序的jstack
使用proccessId
来获取线程,
$ 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和all在内的线程的整个转储。
使用jmc
可视工具
可能会有所帮助: Monitoring queue of ExecutionContextExecutor “scala.concurrent.ExecutionContext.Implicits.global”