Java — Daemon Threads | Code Factory

Code Factory
3 min readJun 2, 2020

--

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.
  • The main objective of Daemon threads is to provide support for non-daemon thread (main thread), for example if main memory runs with law memory then JVM run garbage collector to destroy useless object so that number of bytes of free memory will be improved with these free memory and main thread can continue it’s execution.
  • Usually daemon theads having law priority but based on our environment daemon thread can run with high priority also.
  • We can check daemon nature of a thread by using isDaemon() method of thread class.
/* 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.
  • Changing daemon nature is possible before starting of a thread only. After starting a thread if we are trying to set daemon nature then we will get RE saying IllegalThreadStateException.

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.
  • It is impossible to changing daemon nature of main thread because it is already started by JVM at beggining.
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.
  • If we are not commenting line #1 then main thread is non-daemon and child thread is daemon. Hence whenever main thread terminates automatically child thread will be terminated.

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.
  • Very few operating system like SUN Solarise provide support for green thread model.
  • Any way Green Thread model is deprecared and not recommended to use.

(2) Native OS model

  • The thread which is managed by the JVM with the help of underlying OS is called Native OS model.
  • All windows based OS provide support for 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.
  • We can resume a suspended thread by using resume() method of Thread class then suspended thread can continue it’s execution.
@Deprecated
public final void suspend()
@Deprecated
public final void resume()
  • Anyway these methods are deprecated and not recommended to use.

--

--