Which method is used to create a daemon thread?

Answer:

setDaemon(boolean value) – Set it to “true” to make thread as a daemon thread.

Notes:

It is mandatory to call this method before thread start() method. All threads created by a programmers are user threads/normal threads unless you marked it as a daemon thread using the method setDaemon(boolean value).

Example:

Thread myDaemonThread = new Thread(new Runnable() {
@Override
public void run() {
// DoSomething();
}
});

/* Must call this method before start() */
myDaemonThread.setDaemon(true); /* set it true to mark as Daemon thread*/
myDaemonThread.start();

 



Leave a Comment