Home > Java > javaTutorial > What is the execution priority of java daemon thread?

What is the execution priority of java daemon thread?

PHPz
Release: 2023-04-24 20:31:12
forward
803 people have browsed it

1. Note

The type of thread (user thread or daemon thread) does not affect the priority of thread execution.

The type of thread, whether it is a daemon thread or a user thread, has no impact on the priority of program execution. When we adjust the priority to , the running results of the entire program are completely different.

2. Example

public class DaemonExample {
    private static final int count = 100000;
    public static void main(String[] args) throws InterruptedException {
        // 定义任务
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < count; i++) {
                    System.out.println("执行线程:" + Thread.currentThread().getName());
                }
            }
        };
        // 创建守护线程 t1
        Thread t1 = new Thread(runnable, "t1");
        // 设置为守护线程
        t1.setDaemon(true);
        // 启动线程
        t1.start();
        // 创建用户线程 t2
        Thread t2 = new Thread(runnable, "t2");
        // 启动线程
        t2.start();
    }
}
Copy after login

The above is the detailed content of What is the execution priority of java daemon thread?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template