Why do single threaded Java programs have so many threads?

I have a java program with 13 threads, but only one is 99% CPU utilization and has been running for about 24 hours Other people's CPU utilization is 0.0%, showing that the time is 0:00.0 to 0:12.82, one of which is 3:51.48 The program is designed to be a single threaded program, so I wonder why other threads are there?

What are they doing and why do they show so little CPU usage and time?

Update: I have an old Java program I wrote (the first program – don't judge me!), This is single threaded and shows the same type of thread used

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));
  }
}

Solution

Quote the discussion completed by here and other studies

Several core JVM threads:

>Attach listener: This is a thread that always listens for requests sent by other JVM threads A practical example is in the case of analysis or (I'm not this) production application monitoring tools such as dynatrace. > Signal scheduler: when the operating system sends a signal to the JVM, the signal scheduler thread will pass the signal to the appropriate handler. > Reference handler: a high priority thread used to queue pending references GC creates a simple list of link references that need to be processed, and the thread will quickly add them to the correct queue and notify the ReferenceQueue listener. > Finalizer: the finalizer thread calls the finalizer method. > Destroy Java VM: this thread unloads the Java VM when the program exits Most of the time it should wait. > Garbage collector: the thread responsible for the Java garbage collection mechanism, depending on whether GC is enabled. > Main: the main thread running the program containing the main method

It should be noted that it will depend on the JVM implementation, how many it will start and all core threads, but even if the Java program is written as a single thread, there will be multiple threads in the JVM

Java programs can be single threaded, but the JVM (which will run user-defined Java programs) is multi-threaded, and even if it has multiple threads from the beginning (at least for the latest JVM)

The following is a snapshot of Java hotspot (TM) client VM version 24.55-b03 running a single threaded Java program:

Answer your question

What part: the JVM starts for some purpose. As mentioned above, if any analyzer or monitor wants to get some details from the JVM, the JVM wants to listen Why part: because they are really inactive or running, they are waiting or parked (see the Yellow thread in my attached snapshot. If you have a GUI monitoring application, you should also see yellow, otherwise if the command line and then the thread enters the waiting state). Therefore, they do not occupy any or least CPU cycles, so the CPU utilization is low Time displays the time they were active again, and since they are not, the parameter is less

I hope this can help!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>