What Is a Daemon Thread in Java

How to create a daemon thread? and what for?

First you need to set a thread as daemon just before you start it, so the first thing would be like this:

 Thread t = new Thread(new Evil());
t.setDaemon(true);//success is here now
t.start();
Thread.sleep(1000);

Daemon threads are like normal (user) threads, but there is a big difference. The JVM kills (halt) the application when there is no user thread exist (alive), in other word if you have 1 user thread (main thread for example) and 1000 daemon threads, here the JVM sees one thread in your application, and it kills the application just after that main thread finishes its job.

These threads are good for handling or doing some business logic in the background till other user threads alive, and beware about changing anything withing daemon thread, because there is no any signal before halting a thread by JVM.

So in you case, where daemon thread waits for 1 second and say something and again sleep for 1 second, because this is daemon, and main threads is no more after 1 second, then daemon thread never reaches the second sleep line.

This (diagram) may help you too.
from arashmd.blogspot.com

When are daemon threads useful?

The JVM exits when all the running threads are daemon threads. So imagine you're writing a simple game where your main method loops until you decide to quit. And imagine that at the start of the game, you start a thread that will endlessly poll some website to trigger alerts. You would like the JVM to exit when you decide to end the game. You don't want the endless polling to prevent the game from ending. So you make this polling thread a daemon thread.

What's the difference between user thread and daemon thread in java?

a JVM will exit once the last non-jvm thread terminates. this means that if any of the threads you create is still running, the jvm will not shutdown.
daemon threads are threads that do not prevent the JVM from shutting down. normally you'd use them for some background tasks which you dont want keeping your application up if the user requested it to shut down.

also, your question was already asked (and answered) here - What is Daemon thread in Java?

some common (from personal experience) use cases for daemon threads might include

  • background threads that poll remote systems for status changes
  • background work threads (things like sending out email notifications, snmp, whatever)
  • custom timer threads meant to perform scheduled maintainance

java daemon thread and non-daemon thread

A. When an application begins running, there is one daemon thread, whose job is to execute main().

This is incorrect. See below.

B. When an application begins running, there is one non-daemon thread, whose job is to execute main().

Correct. The JVM exits when the last non-daemon thread exits. If the main thread wasn't non-daemon then the JVM would start up and see that there were no non-daemon threads running and would shutdown immediately.

So therefore the main thread must be a non-daemon thread. For a description of the different between daemon and non, see my answer here: Difference between a daemon thread and a low priority thread

C. A thread created by a daemon thread is initially also a daemon thread.

D. A thread created by a non-daemon thread is initially also a non-daemon thread.

Both are correct. The thread gets its daemon status from the thread that spawned it by default. Daemon threads spawn other daemon threads. Non-daemon threads spawn other non-daemon threads. Looking at the code from Thread.init():

Thread parent = currentThread();
...
this.daemon = parent.isDaemon();

If you want to change the daemon status then you have to do so before the thread is started.

Thread thread = new Thread(...);
// thread has the daemon status of the current thread
// so we have to override it if we want to change that
thread.setDaemon(true);
// we need to set the daemon status _before_ the thread starts
thread.start();

Eclipse Debugger: Threads vs Daemon Threads

Daemon threads in Java are threads that run in the background (mostly created by the JVM) for performing background tasks (like garbage collection). The main difference between a daemon thread and a user thread is that as soon as all user threads finish execution Java terminates itself. JVM doesn't wait for daemon threads to finish their execution.

Note that you can make a thread created by a user thread to be a daemon thread by setDaemon(true) (and it must be called before the thread's start() method is called).

In order for a program to continue running, it must always have at least one live user thread.

Eclipse, like you, can easily check whether a thread isDaemon() or not.

java threading - Daemon thread?

The main difference between a daemon thread and a non-daemon thread is that a program terminates when all non-daemon threads have terminated. So if you've got an active daemon thread and end your first thread, the program terminates. So you'd want to use a daemon thread for something you want to keep doing as long as the program is running.

Why is it bad to call Join on a daemon thread

The relevance of a daemon thread, and its definition, is that it doesn't prevent the JVM from exiting when the program finishes but the thread is still running.

For any thread, designed to run and end before the program finishes, it is useless to be a daemon thread.

From this it is logical to conclude that any well designed daemon thread is designed to run as long as the program runs.

Joining on a daemon thread, therefore, implies the join will block until the daemon thread ends, which, assuming it is a well designed daemon thread, is never. If this blocking join() prevents further useful code to be executed, that will never happen, and possibly, your code will be stuck.



Related Topics



Leave a reply



Submit