gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:24:36 24 4
gpt4 key购买 nike

例如,下面的代码为什么没有输出优先级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

最佳答案

new Thread(new A());

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

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

您应该直接使用new A()

关于java - 为什么新线程内 getPriority 的优先级与调用线程中的优先级不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com