【问题标题】:Why does the priority from getPriority inside a new thread differ from the priority in the calling thread?为什么新线程中 getPriority 的优先级与调用线程中的优先级不同?
【发布时间】:2014-10-28 02:37:11
【问题描述】:

比如下面的代码为什么不输出优先级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

【问题讨论】:

    标签: java multithreading java-7 thread-priority


    【解决方案1】:
    new Thread(new A());
    

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

    新的Thread 实例根本不会影响其RunnableThread 基础。

    你应该直接使用new A()

    【讨论】:

      猜你喜欢
      • 2011-06-25
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多