Why does one streaming java program have so many threads?

I have a java program that has 13 threads, although only one of them works with a 99% processor and runs for ~ 24 hours. The rest work with 0.0% cpu and show TIME+ somewhere from 0:00.0 to 0:12.82 , and one has 3:51.48 . The program is designed for a single-threaded program, so I wonder why there are other threads?

What do they do and why do they show so little CPU usage and TIME +?

UPDATE: I have an old java program that I wrote (the first program - don't judge me!), Which is single-threaded and shows the same type of using threads ...

 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)); } } 
+6
source share
4 answers

To quote a discussion here and other studies.

Some of the main JVM threads are:

  • Attach Listener: This is a thread that always listens on other JVM threads to send a request. A practical example is in the case of profiling or (I'm not talking about that though) monitoring tools for production-level applications such as DynaTrace.
  • Signal Manager: When the OS picks up a signal on the JVM, the signal manager thread passes the signal to the appropriate handler.
  • Help handler: High priority thread for queue for pending links. GC creates a simple linked list of links that need to be processed, and this thread quickly adds them to the desired queue and notifies ReferenceQueue listeners.
  • Finalizer: The Finalizer thread calls finalizer methods.
  • DestroyJavaVM:. This thread unloads the Java virtual machine at the output of the program. Most of the time he has to wait.
  • Garbage Collector: The topic responsible for the Java garbage collection mechanism, depending on whether the GC is enabled or not.
  • main: The main thread executing your program containing the main method.

One important thing to note is that it will depend on the JVM implementation, how many and which all the main threads it will start, but even if the Java program is written as single-threaded, more than one thread in the JVM.

A Java program can be single-threaded, but the JVM (which launches the Java user program) is multithreaded and will have (at least for the latest JVMs) more than one thread, even from the very beginning.

The following is a snapshot of my Java HotSpot (TM) 24.55-b03 client virtual machine running a single-threaded Java program:

To answer your request

What do they do and why do they show so little CPU usage and TIME +?

Which part:. The JVM starts for a specific purpose, as described above, how the JVM wants to listen if any profiling or monitoring program wants to get some details from the JVM.
Why part:. Since they are really inactive or running, they are in a standby or parked state (see Yellow topics in my attached picture, if you have a GUI monitoring application, then you should also see Yellow or else if command line, and then threads in WAIT state), and therefore they do not take any or less processor cycles and therefore less CPU usage. Again TIME+ will show you the time when they were active, and since it is not, this parameter is also smaller.

Hope this helps!

enter image description here

+4
source

Running a Java program means starting the JVM and specifying which core class to run (usually this is a static core method).

This JVM generates several background threads in addition to the aforementioned main thread.

Among them

  • VM thread : A monitoring thread waiting for tasks that require the virtual machine to be in a safe place. For example, there is a garbage collection task that completely β€œstops the world”. But there are others.
  • GC threads : multiple threads that are supported to run the garbage collection.
  • Compile streams . These threads are used to compile byte code into native machine code.

There may be many.

In addition, if you use AWT or Swing, you will get some more threads from these frameworks. One of them is the so-called Event Manager (EDT). And, of course, there may be a thread that you created and started: timers, executors, or just arbitrary threads. Even for a simple hello world application, a dozen threads can be launched.

But most of these threads are more waiting than doing something. Thus, the chances are high that only one thread really works, thereby using some processor.

Although ... 100% processor utilization may be an indicator of some problem. For example, an infinite loop. You must use a profiler to find out what is actually happening. But it may just be a program that has such processor usage. Your choice.

+10
source

Most likely, the threads were created somewhere and are never used.

For instance:

 ExecutorService es = Executors.newFixedThreadPool(12); // burn cpu, using only one thread (main) int i = 0; while(true) { i++; } 
+2
source

TIME+ above is the amount of processor time spent. One explanation is that the process is constantly blocking or just blocking, because it will have both low CPU usage and low TIME+ .

+1
source

Source: https://habr.com/ru/post/989683/


All Articles