- Details
- Written by Nam Ha Minh
- Last Updated on 12 August 2019   |   Print Email
This tutorial helps you understand about thread priorities and daemon thread in Java concurrent programming.
1. Thread Priorities in Java
Each thread has a priority value that hints the thread scheduler how much it should be cared in case of many threads are running.When the thread scheduler needs to pick a thread to run next among the waiting ones, it prefers the thread with highest priority. By default, a new thread has same priority as the thread created it. For example, if you create a new thread from the main method, the new thread has priority equal to the main thread’s priority.You can set priority of a thread by calling
Thread.setPriority(int priorityLevel). The
priorityLevel ranges from minimum value (
Thread.MIN_PRIORITY = 1) to maximum value (
Thread.MAX_PRIORITY = 10). The
Thread class also defines a constant indicating normal priority (
Thread.NORM_PRIORITY = 5). The
Thread class defines 3 constants to represent 3 priority levels:
- Thread.MIN_PRIORITY = 1
- Thread.NORM_PRIORITY = 5
- Thread.MAX_PRIORITY = 10
However you can set any value from 1 to 10.For example, the following code sets priority of thread
t1 to a level above normal and priority of thread
t2 to maximum:
Thread t1 = newThread;
t1.setPriority(8);
Thread t2 = newThread;
t2.setPriority(Thread.MAX_PRIORITY);
To check priority of a thread, call
getPriority() method. For example, the following code gets the priority of the current thread:
int priority = Thread.currentThread().getPriority();
If the threads have same priority, it’s up to for thread schedule to pick which one first (randomly).Be careful when using thread priorities, because starvation can occur when low-priority threads do not have chance to run because the high-priority threads take all the CPU time.And don’t overuse thread priorities because they are mapped to thread priorities of the host operating system, and each operating system treats thread priorities differently. So never structure your program relying on thread priorities.
2. Daemon Thread
Java defines two types of thread: user thread (normal thread) and daemon thread. By default, when you create a new thread it is user thread. The Java Virtual Machine (JVM) won’t terminate if there are still user threads running. But it will exit if there are only daemon threads running.Daemon threads have lower priority than normal ones, so they are used for running background services that serve user threads. An example of daemon thread in the JVM is the garbage collector thread that runs silently in the background to free unused memory.You can make a thread daemon by calling
Thread.setDaemon(true) and check daemon status by using
isDaemon(). Note that
setDaemon() should be called before the thread is started. Here’s an example:
Thread daemonThread = new Thread(new Runnable() {
public void run() {
// do something
}
});
daemonThread.setDaemon(true);
daemonThread.start();
A thread inherits daemon status from its parent thread - the one that created it. The purpose of daemon threads is serving user threads, there’s no need to keep daemon threads running if all user threads terminate. That’s why the JVM will exit if there are only daemon threads running.For example, the following program helps you understand the concept of daemon thread:
public class DaemonThread {
public static void main(String[] args) {
Thread userThread = new Thread(new Runnable() {
public void run() {
int x = 10;
while (x > 0) {
System.out.println("User thread: " + x--);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { ex.printStackTrace(); }
}
}
});
userThread.start();
Thread daemonThread = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println("Daemon thread is running...");
try {
Thread.sleep(100);
} catch (InterruptedException ex) { ex.printStackTrace(); }
}
}
});
daemonThread.setDaemon(true);
daemonThread.start();
}
}
This program creates and starts two threads:
userThread and
daemonThread. Look at the
run() method of the
daemonThread class you see that it will run forever (indefinite while loop). And the
run() method of the
userThread class will run for only 10 seconds.Run this program and you will see it will terminate after 10 seconds when the
userThread terminates, despite that the
daemonThread is still running. That proves the JVM exits when only daemon threads are running.
Related Java Thread Tutorials:
Other Java Concurrency Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on
Facebook and watch
his Java videos you YouTube.