Thursday, January 14, 2021

How do you create a daemon thread?

Creating a thread as a daemon in Java is as simple as calling the setDaemon() method. 

A setting of true means the thread is a daemon; false means it is not. By default, all threads are created with an initial value of false.

Java Daemon Thread Example

  1. You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process.

  2. Daemon threads will be terminated by the JVM when there are none of the other threads running, it includes main thread of execution as well.

  3. To specify that a thread is a daemon thread, call the setDaemon() method with the argument true.

  4. To determine if a thread is a daemon thread, use the accessor method isDaemon().

Have a go - run the script above and you will be able to answer this question :)

No comments:

Post a Comment