为什么新线程中getPriority的优先级与调用线程中的优先级不同?

时间:2014-10-28 02:37:11

标签: java multithreading java-7 thread-priority

例如,为什么以下代码不输出7的优先级?

public class Test {
    public static void main(String[] args) {
        Thread thread = new Thread(new A());
        thread.setPriority(7);
        System.out.println("in main: " + thread.getPriority());
        thread.start();
    }
}

class A extends Thread {
    @Override
    public void run() {
        System.out.println("in thread: " + this.getPriority());
    }
}

输出:

in main: 7
in thread: 5

1 个答案:

答案 0 :(得分:4)

new Thread(new A());

您将new A()视为Runnable并将其传递给单独的Thread个实例。

新的Thread实例根本不会影响其Thread的{​​{1}}基础。

您应该直接使用Runnable