Java — Daemon Threads | Code Factory

Index Page : Link

Donate : Link

WordPress Blog : Link

  • The threads which are executing in background are called Daemon threads. Ex. (1) Garbage Collector, (2) Signal Dispatcher, (3) Attach Listener.
/* Tests if this thread is a daemon thread. */
public final boolean isDaemon()
  • We can change daemon nature of a thread by using setDaemon() method.
/* Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
This method must be invoked before the thread is started.
true - marks this thread as a daemon thread
false - marks this thread as a non-daemon/user thread*/
public final void setDaemon(boolean on)
  • * main thread is non-daemon thread.

Default Nature :

  • By default main thread is always non-daemon and for all remaining threads nature will be inherited from parent to child that is if the parent thread is daemon then automatically child thread is also daemon. And if the parent is non-daemon then automatically is also non-daemon.
package com.example.thread;/**
* @author code.factory
*
*/
public class DaemonTest extends Thread {
public static void main(String... args) {
System.out.println(Thread.currentThread().isDaemon());
//Thread.currentThread().setDaemon(true); // RE : IllegalThreadStateException
DaemonTest d = new DaemonTest();
System.out.println(d.isDaemon());
d.setDaemon(true);
System.out.println(d.isDaemon());
}
}

Output :

false
false
true
  • Whenever last non-daemon thread terminates automatically all daemon threads will be terminated irrespictive of their position.
package com.example.thread;/**
* @author code.factory
*
*/
public class DaemonTest {
public static void main(String... args) {
MyThread t = new MyThread();
t.setDaemon(true); // #1
t.start();
System.out.println("Main End");
}
}
class MyThread extends Thread {
public void run() {
for(int i=0; i<5; i++) {
System.out.println("Child Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Output :

Main End
Child Thread
  • If we are commenting line #1 then both main and child threads are non-daemon and hence both threads will execution until their completion.

1. Java multithreading concept is implemented by using following 2 models.

(1) Green Thread model

  • The thread which is managed completely by JVM without taking underlying OS support is called Green Thread.

(2) Native OS model

  • The thread which is managed by the JVM with the help of underlying OS is called Native OS model.

2. How to stop a thread

  • We can stop a thread execution by using stop() method of Thread class.
@Deprecated
public final void stop()
  • If we call stop() method then immediately thread will entered into dead state. Anyway stop() method is deprecated and not recommended to use.

3. How to suspend and resume of a thread

  • We can suspend a thread by using suspend() method of Thread class then immediately the thread will be entered into suspended state.
@Deprecated
public final void suspend()
@Deprecated
public final void resume()
  • Anyway these methods are deprecated and not recommended to use.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store