Interview Questions

What are daemon threads?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

24. What are daemon threads?

Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.
12 Few salient features of daemon() threads>

Thread scheduler schedules these threads only when CPU is idle.

Daemon threads are service oriented threads, they serves all other threads.

These threads are created before user threads are created and die after all other user threads dies.

Priority of daemon threads is always 1 (i.e. MIN_PRIORITY).

User created threads are non daemon threads.

JVM can exit when only daemon threads exist in system.

we can use isDaemon() method to check whether thread is daemon thread or not.

we can use setDaemon(boolean on) method to make any user method a daemon thread.

If setDaemon(boolean on) is called on thread after calling start() method than IllegalThreadStateException is thrown.

You may like to see how daemon threads work, for that you can use VisualVM or jStack. I have provided Thread dumps over there which shows daemon threads which were intermittently running in background.

Some of the daemon threads which intermittently run in background are :
"RMI TCP Connection(3)-10.175.2.71" daemon
"RMI TCP Connection(idle)" daemon
"RMI Scheduler(0)" daemon
"C2 CompilerThread1" daemon
"GC task thread#0 (ParallelGC)"

(Continued on next question...)

Other Interview Questions