/ Java  

Java Multithreading 8: Thread priority and daemon threads

In this blog, we will introduce daemon threads and thread priorities. The contents include:

  1. Introduction to thread priority
  2. Examples of thread priority
  3. Examples of daemon threads

Introduction to thread priority

The thread priority in java ranges from 1 to 10, and the default priority is 5. High-priority threads will take precedence over low-priority threads.

There are two kinds of threads in java: user threads and daemon threads. They can be distinguished by the isDaemon() method: if false is returned, the thread is a user thread; otherwise it is a daemon thread.

User threads generally perform user-level tasks, while daemon threads are also known as “ackground threads and are generally used to perform background tasks. It should be noted that the Java virtual machine will exit after all user threads have ended.

Description from the JDK official document (https://docs.oracle.com/cd/E17802_01/j2se/j2se/1.5.0/jcp/beta1/apidiffs/java/lang/Thread.html):

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method

Examples of thread priority

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class PriorityDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "(" + Thread.currentThread().getPriority() + ")");

Thread t1 = new MyThread("t1");
Thread t2 = new MyThread("t2");
// set priority
t1.setPriority(1);
t2.setPriority(10);
t1.start();
t2.start();
}
}

class MyThread extends Thread {
public MyThread(String name) {
super(name);
}

public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ "(" + Thread.currentThread().getPriority() + ")"
+ ", loop " + i);
}
}
}

Results:

1
2
3
4
5
6
7
8
9
10
11
main(5)
t1(1), loop 0
t2(10), loop 0
t1(1), loop 1
t2(10), loop 1
t1(1), loop 2
t2(10), loop 2
t1(1), loop 3
t2(10), loop 3
t1(1), loop 4
t2(10), loop 4

The priority of the main thread is 5.

The priority of t1 is set to 1, and the priority of t2 is set to 10. When the CPU executes t1 and t2, it is scheduled according to the time slice, so it can be executed concurrently.

Examples of daemon threads

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class DaemonDemo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + "(isDaemon=" + Thread.currentThread().isDaemon() + ")");

Thread t1 = new MyThread2("t1");
Thread t2 = new MyDaemon("t2");

t2.setDaemon(true);
t1.start();
t2.start();
}
}

class MyThread2 extends Thread {
public MyThread2(String name) {
super(name);
}

public void run() {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(3);
System.out.println(this.getName() + "(isDaemon=" + this.isDaemon() + ")" + ", loop " + i);
}
} catch (InterruptedException e) {
}
}
};

class MyDaemon extends Thread {
public MyDaemon(String name) {
super(name);
}

public void run() {
try {
for (int i = 0; i < 10000; i++) {
Thread.sleep(1);
System.out.println(this.getName() + "(isDaemon=" + this.isDaemon() + ")" + ", loop " + i);
}
} catch (InterruptedException e) {
}
}
}

The main thread is a user thread, and the sub-thread t1 it creates is also a user thread.

t2 is a daemon thread. When the main thread and t1 (they are both user threads) are executed, and only the daemon thread t2 remains, the JVM automatically exits.