Nicolas S.Xu
Nicolas S.Xu

Reputation: 14524

Why JVM needs to exit?

I am researching the "what is deamon thread in java"

It mentioned "A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running".

My follow up question is why does JVM need to exit, or not exit? What is going on? I think JVM is always running. Is it true? Why should I care if JVM is doing?

Upvotes: 0

Views: 580

Answers (5)

Bruce
Bruce

Reputation: 8849

Deamon thread is like a utility thread that support your execution. For example memory management is a Deamon thread that support your process memory management. After your process finished it will run forever if it is not a Deamon thread. So your process is never going to finish. But JVM should stop after you have finished the process. So JVM stops when your system only running deamon threads.

public class ThreadExample {
    public static void main(String args[]){
        new DeamonThread().start();
        for(int i=0;i<5;i++){
            System.out.println("Main thread is running "+ i+" time");
        }

    }
}

class DeamonThread extends Thread{
    DeamonThread(){
        setDaemon(true);
    }
    public void run(){
        int count=0 ;
        while (true) {
            System.out.println("Hello from Deamon Thread "+count++) ;
            try {
                sleep(1000);
            } catch (InterruptedException e) {}
        }
    }
}

In this example main thread only consist of 5 iteration. But Deamon thread has infinite iterations. If it is not a deamon thread it will run forever. But it is a Deamon thread so JVM stops when main thread(not a deamon thread) finishes.

Upvotes: 1

Boann
Boann

Reputation: 50021

No, the JVM is not always running. Usually it is not running at all. When you start a Java-based program, that starts an instance of the JVM process. If you start several Java-based programs, or several instances of one program, then you get several JVMs. They are not re-used, so old ones are useless and need to go away when your application is done, to prevent eating memory. Look at your system task manager and you will see when the JVM is running, and how many are running.

Even if there were a single JVM that is always running, your application's threads still need to terminate once your application's job is done, otherwise the threads would still be wasting memory and resources.

So regardless of whether the JVM is always running or not, being able to set threads as daemon threads is still useful, because you don't have to remember to manually stop them. Daemon threads stop themselves once the non-daemon threads are done.

Upvotes: 1

Abhishek
Abhishek

Reputation: 878

Daemon threads are helping threads, Garbage collection for example is a daemon thread.

A daemon thread shouldn't be holding any resources. If you are contravening this basic rule then your thread doesn't qualify for being a daemon thread.

After you have finished running all the threads and only daemon threads are remaining the JVM should stop as no other threads are pending in the thread pool.

Upvotes: 1

initramfs
initramfs

Reputation: 8405

public class Example{
    public static void main(String[] args){
        System.out.println("Hi!");

        // What's happening here?
    }
}

Look at the comment that states what's happening here? What is happening there? The JVM is about the exit as your program is about to complete. The java virtual machine only lasts as long as your program does, it starts when your program starts and ends when your program ends.

Now if we consider:

public class Example{
    public static void main(String[] args){
        System.out.println("Hi!");

        Thread t = new Thread(new Runnable(){
            public void run(){
                try{
                    Thread.sleep(Long.MAX_VALUE);
                }catch(InterruptedException ex){}
            }
        });

        t.start();

        // What's happening here?
    }
}

What will happen at the point where the comment exists. The answer is nothing. As another thread is still alive and non-daemon, the program continues to run (and thus the JVM does not exit). The only way then to have it end is either have the sleep complete or manually terminating the process running the application.

In the most simplistic terms (and not exactly the most correct if you're pedantic) the running life of the JVM for a java application can be equated to the running life of the application itself. For most situations you can substitute the term JVM for "my application" without much issue.

Hope that clears it up.

Upvotes: 2

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

You might be confusing a daemon thread with a daemon process. You can easily run the JVM as a daemon process when you need it (ie you're running a server). This is very similar to running any other process as a daemon in most operating systems. A Java daemon thread however is something different, you don't use those generally to run server-specific tasks. You might use them for cleanup tasks for example.

Upvotes: 0

Related Questions