美文网首页
Java 线程优先级设置

Java 线程优先级设置

作者: 西贝巴巴 | 来源:发表于2021-03-22 15:24 被阅读0次
package com.company;

//通过setPriority() 方法来设置线程的优先级
public class ThreadTest5 extends Thread {
    private int countDown = 5;
    private volatile double d = 0;

    public ThreadTest5(int priority) {
        setPriority(priority);
        start();
    }

    public String toString() {
        return super.toString() + ": " + countDown;
    }

    public void run() {
        while (true) {
            for (int i = 1; i < 100000; i++)
                d = d + (Math.PI + Math.E) / (double) i;
            System.out.println(this);
            if (--countDown == 0) return;
        }
    }

    public static void main(String[] args) {
        new ThreadTest5(Thread.MAX_PRIORITY);
        System.out.println("Thread.MAX_PRIORITY: " + Thread.MAX_PRIORITY);
        for (int i = 0; i < 5; i++) {
            new ThreadTest5(Thread.MIN_PRIORITY);
            System.out.println("cc Thread.MAX_PRIORITY: " + Thread.MAX_PRIORITY);
        }
    }
}

相关文章

网友评论

      本文标题:Java 线程优先级设置

      本文链接:https://www.haomeiwen.com/subject/rakcqltx.html