Understanding Thread Priority & Daemon Thread in Java

In this tutorial, we will understand the Thread Priority and Daemon Thread in Java. Before starting I would suggest reading Multithreading in Java

1. Thread Priority in Java

Let’s suppose you’re running multiple threads in your application. So, defining thread priorities determines how much that particular thread should be considered during the lifecycle of a thread.

The thread scheduler is responsible for doing all these activities. Next time, when thread scheduler is going to run threads, it will decide which thread have higher priority and thread scheduler will execute that particular thread.

All threads have some default priorities and we can also set them manually. By default when a new thread is created in the memory, it inherits the priority of its parent.

Also, you can set thread priorities manually by Thread class method setPriority(int priorityLevel). This method accepts Integer values ranging from 1 to 10.

1.1 How to set priority of thread in java?

Thread class defines some default constant to define priority level:

  1. Thread.MIN_PRIORITY = 1
  2. Thread.NORM_PRIORITY = 5
  3. Thread.MAX_PRIORITY = 10

Let’s take an example:

Thread t1 = new Thread();
t1.setPriority(7); // setting manually

Thread t2 = new Thread();
t2.setPriority(Thread.MIN_PRIORITY); // setting through constant

If you wish to get priority of any thread then you can you .getPriority() method of Thread class. For Example:

int priority = Thread.currentThread().getPriority();

1.2 What if Threads have the same Priority?

In that case, thread scheduler will pick thread randomly. But never set thread priorities too low because that might be the case low priority thread will never get chance to execute due to high priority thread.

Example:

public class DemoClass extends Thread {

	@Override
	public void run() {
		System.out.println("Thread " + " with priority " + Thread.currentThread().getPriority() + " is runnning ");
	}
	public static void main(String[] args) {
		DemoClass thread1 = new DemoClass();
		thread1.setPriority(4);

		DemoClass thread2 = new DemoClass();
		thread2.setPriority(10);

		thread1.start();
		thread2.start();
	}
}

Output:

Thread  with priority 10 is runnning 
Thread  with priority 4 is runnning

2. Daemon Thread in Java

As we discussed in Multithreading in Java, a thread is a light weighted subprocess of any program that runs simultaneously with other threads. In Java, there are two types of threads

  1. User Thread (Normal Thread)
  2. Daemon Thread

Let’s understand,

While running any application there are numerous threads running in the background, responsible for doing certain background tasks like garbage collection and they are called Daemon Thread.

Daemon threads are used to serve user thread and they have the lowest priority. Also, if no user thread is running and JVM finds only Daemon thread running then JVM will stop Daemon thread and terminates the program.

Remember:

The main method is non-daemon by nature. If you create any thread in the main method, it will be non-daemon because it will inherit the property of its parent i.e main method.

Therefore, all child thread of the main thread will be daemon in nature.

However, You can also make a user thread as a daemon by Thread.setDaemon(true).

Example:

See below example to see how to use setDaemon() and isDaemon() method:

public class DemoClass extends Thread {
	public void run() {

		if (Thread.currentThread().isDaemon()) {
			System.out.println("Daemon thread executing");
		} else {
			System.out.println("User thread executing");
		}
	}
	public static void main(String[] args) {
		/* By default these are user threads (non-daemon threads) */
		DemoClass thread1 = new DemoClass();
		DemoClass thread2 = new DemoClass();

		// Making user thread thread1 to Daemon
		thread1.setDaemon(true);

		// starting both the threads
		thread1.start();
		thread2.start();
	}
}

Output:

Daemon thread executing
User thread executing

I hope you’ve understood What is Thread Priority in Java and What is Daemon Thread in Java.