为什么单线程java程序有这么多线程? [英] Why does a single threaded java program have so many threads?

查看:130
本文介绍了为什么单线程java程序有这么多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java程序,它有13个线程,但只有一个是99%的CPU使用率并且已经运行了大约24小时。其他人的cpu使用率为0.0%,显示 TIME + 的任何地方,从 0:00.0 0:12.82 ,其中一个 3:51.48 。该程序是一个单线程程序,所以我想知道为什么其他线程在那里?

I have a java program which has 13 threads, though only one of them is at 99% cpu usage and has been running for ~24 hours. The others are at 0.0% cpu usage and show a TIME+ of anywhere from 0:00.0 to 0:12.82 and one has 3:51.48. The program is intended to be a single threaded program, so I'm wondering why the other threads are there?

他们在做什么,为什么他们显示这么少的CPU用法和TIME +?

What are they doing and why do they show so little cpu usage and TIME+?

更新:我有一个旧的java程序我写的(第一个程序 - 不要判断我!)这是单线程并显示相同类型的线程使用...

UPDATE: I have an old java program I wrote (first program - don't judge me!) which is single threaded and shows the same type of thread usage ...

import java.io.*;

class xdriver {
  static int N = 100;
  static double pi = 3.141592653589793;
  static double one = 1.0;
  static double two = 2.0;

  public static void main(String[] args) {
    //System.out.println("Program has started successfully\n");

    if( args.length == 1) {
      // assume that args[0] is an integer
      N = Integer.parseInt(args[0]);
    }   

    // maybe we can get user input later on this ...
    int nr = N;
    int nt = N;
    int np = 2*N;

    double dr = 1.0/(double)(nr-1);
    double dt = pi/(double)(nt-1);
    double dp = (two*pi)/(double)(np-1);

    System.out.format("nn --> %d\n", nr*nt*np);

    if(nr*nt*np < 0) {
      System.out.format("ERROR: nr*nt*np = %d(long) which is %d(int)\n", (long)( (long)nr*(long)nt*(long)np), nr*nt*np);
      System.exit(1);
    }   

    // inserted to artificially blow up RAM
    double[][] dels = new double [nr*nt*np][3];

    double[] rs = new double[nr];
    double[] ts = new double[nt];
    double[] ps = new double[np];

    for(int ir = 0; ir < nr; ir++) {
      rs[ir] = dr*(double)(ir);
    }   
    for(int it = 0; it < nt; it++) {
      ts[it] = dt*(double)(it);
    }   
    for(int ip = 0; ip < np; ip++) {
      ps[ip] = dp*(double)(ip);
    }   

    double C = (4.0/3.0)*pi;
    C = one/C;

    double fint = 0.0;
    int ii = 0;
    for(int ir = 0; ir < nr; ir++) {
      double r = rs[ir];
      double r2dr = r*r*dr;
      for(int it = 0; it < nt; it++) {
        double t = ts[it];
        double sint = Math.sin(t);
        for(int ip = 0; ip < np; ip++) {
          fint += C*r2dr*sint*dt*dp;

          dels[ii][0] = dr; 
          dels[ii][1] = dt; 
          dels[ii][2] = dp; 
        }   
      }   
    }   

    System.out.format("N ........ %d\n", N); 
    System.out.format("fint ..... %15.10f\n", fint);
    System.out.format("err ...... %15.10f\n", Math.abs(1.0-fint));
  }
}


推荐答案

引用讨论已完成此处和其他研究。

Quoting the discussion done here and other research.

几乎没有核心JVM线程:

Few of core JVM threads:


  1. 附加监听器:这是始终侦听其他JVM线程的线程发送请求。一个实际的例子是分析或(我不是这个)生产级应用程序监视工具,如DynaTrace。

  2. 信号调度程序:当操作系统引发时发送到JVM的信号,信号调度程序线程将信号传递给适当的处理程序。

  3. 引用处理程序:高优先级线程,用于将待处理的引用排入队列。 GC创建一个简单的链接引用列表,需要进行处理,并且该线程会快速将它们添加到正确的队列中并通知ReferenceQueue侦听器。

  4. 终结器: Finalizer线程调用终结器方法。

  5. DestroyJavaVM:此线程在程序退出时卸载Java VM。大部分时间都应该等待。

  6. 垃圾收集器:负责Java垃圾收集机制的线程,具体取决于GC是否启用。

  7. main:运行程序的主线程包含 main 方法。

  1. Attach Listener: This is thread which always listens for other JVM threads to send a request. A practical example is in case of profiling or (I am not about this though) production level application monitoring tools like DynaTrace.
  2. Signal Dispatcher: When the OS raises a signal to the JVM, the signal dispatcher thread will pass the signal to the appropriate handler.
  3. Reference Handler: High-priority thread to enqueue pending References. The GC creates a simple linked list of references which need to be processed and this thread quickly adds them to a proper queue and notifies ReferenceQueue listeners.
  4. Finalizer: The Finalizer thread calls finalizer methods.
  5. DestroyJavaVM: This thread unloads the Java VM on program exit. Most of the time it should be waiting.
  6. Garbage Collector: Thread responsible for Java's garbage collection mechanism, depending upon if GC is enabled or not.
  7. main: Main thread running your program containing main method.

需要注意的一个要点是,它将依赖于JVM实现它将启动多少以及所有核心线程,但即使编写了Java程序要成为单线程,JVM中会有多个线程。

One important point to note is that it will depend upon JVM implementations that how many and which all core threads it will start but even if Java program is written to be a single threaded, there would be more than one thread in JVM.

Java程序可以是单线程但JVM(将运行用户定义的Java程序) )是多线程的,即使从一开始就会有(至少对于最新的JVM)多个线程。

下面是我的Java的快照HotSpot(TM)客户端VM版本24.55-b03,运行单线程Java程序:

Below is a snapshot of my Java HotSpot(TM) Client VM version 24.55-b03, running a single threaded Java program:

回答您的查询

他们在做什么,为什么他们显示如此少的CPU使用率和
TIME +?

What are they doing and why do they show so little cpu usage and TIME+?

什么部分: JVM开始有一个目的,如上所述,如果任何分析或监控程序想要从JVM获取一些细节,JVM想要监听。

为什么要分开:因为它们真的没有活动或正在运行,它们处于等待或停放状态(请参阅我附加快照中的黄色线程,如果你有一个GUI监控应用程序,那么你也应该看到黄色,否则命令行然后处于WAIT状态的线程)因此它们不占用任何或最少的CPU周期,因此CPU使用率较低。
再次 TIME + 将显示他们活跃的时间,因为它们不是,所以此参数也更少。

What part: There are started for a purpose by JVM, as explained above like JVM wants to listen if any profiling or monitoring program wants to gets some details from JVM.
Why part: Because they are really not active or running, they are in wait or parked state (see the Yellow threads in my attached snapshot, if you have a GUI monitoring app then you should also see Yellow or else if command line then threads in WAIT state) and so they are not occupying any or least CPU cycles and hence less CPU usage. Again TIME+ will show you time for they have been active, and since they are not, this parameter is also less.

希望这会有所帮助!

这篇关于为什么单线程java程序有这么多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆