Contents of page :
- Daemon threads >
- 12 salient features of daemon() threads >
- Program
Daemon threads >
Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.
12 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.
- Daemon threads are low priority threads which runs intermittently in background for doing garbage collection.
- 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. Generally, We must not make user threads as daemon.
- 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)"
|
- Now, Lets create program >
Program to create daemon thread by using setDaemon() method and also use isDaemon() method to check whether thread is daemon or not.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DaemonTest {
public static void main(String[] args) throws InterruptedException {
final Thread thread1=new Thread("Thread-1"){
public void run() {
System.out.println(Thread.currentThread().getName()+" has started");
System.out.println(Thread.currentThread().getName()+" has ended");
}
};
thread1.setDaemon(true); //setting thread to daemon.
System.out.println("is thread1 daemon thread : "
+thread1.isDaemon()); //checking thread isDeamon ?
thread1.start(); //start daemon thread
}
}
/*
is thread1 daemon thread : true
Thread-1 has started
Thread-1 has ended
*/
|
Program to show > if setDaemon(boolean on) is called on thread after calling start() method than IllegalThreadStateException is thrown.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DaemonTestException {
public static void main(String[] args) throws InterruptedException {
final Thread thread1=new Thread("Thread-1"){
public void run() {
System.out.println(Thread.currentThread().getName()+" has started");
System.out.println(Thread.currentThread().getName()+" has ended");
}
};
thread1.start(); //start daemon thread
thread1.setDaemon(true); //setting thread to daemon after start(), will throw IllegalThreadStateException.
}
}
/*
Thread-1 has started
Thread-1 has ended
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.setDaemon(Unknown Source)
at DaemonTestException.main(DaemonTestException.java:13)
*/
|
RELATED LINKS>
Thread basics >
What is thread in java
Thread states/ Thread life cycle in java
Difference between Process and Thread in java
Implementing Threads in java by implementing Runnable interface and extending Thread class
Threads implement their own stack - demonstration using program and diagram
Differences between implementing Runnable interface and extending Thread class
Thread behaviour is unpredictable
When threads are not lightweight process in java, in fact they become heavy weight process
Important Thread methods (salient features, usage with programs)>
Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join
Sleep() method in threads - 10 key features with programs
Yield() method in threads - 8 key features with programs
Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.
Daemon threads - 12 salient features of Daemon Thread
2 alternate ways to stop thread, as stop() method is deprecated
Using Suspend and resume method in threads
Differences between important thread methods >