Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my single threaded hello world app use 22 OS threads?

Tags:

java

jvm

Out of curiosity I wrote Hello World and set a break point on my print statement.

public class Program
{
    public static void main(String[] args)
    {
        System.out.println("Hello");
    }
}

When the break point was hit, I pulled up Task Manager in windows to see how many threads were allocated by that JVM process. I was shocked to see 22. Why are there so many threads spawned for this simple program?

like image 694
Dejas Avatar asked Feb 14 '14 18:02

Dejas


People also ask

How many threads are running in Hello World program?

It calls the thread-creator twice, once for "hello" and once for "world." The result is again that "hello" and "world" each appear 5 times on the screen.

How many threads can be executed at a time?

Right Answer is: In the operating system, only one thread is executed at a time.

Is JVM single threaded?

The OS sees JVM as a single process and a single thread. Therefore, any thread created by JVM is supposed to be maintained by it only. Green threads hold all the information related to the thread within the thread object itself.

How do threads work?

Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.


1 Answers

The JVM utilizes some daemon thread (e.g. garbage collector, finalizer, etc...). However, in this case, attaching a debugger has actually created a few more threads to handle the debuggers connection.

like image 149
Tim Bender Avatar answered Sep 25 '22 17:09

Tim Bender