例如,为什么以下代码不输出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
答案 0 :(得分:4)
new Thread(new A());
您将new A()
视为Runnable
并将其传递给单独的Thread
个实例。
新的Thread
实例根本不会影响其Thread
的{{1}}基础。
您应该直接使用Runnable
。