public class Thread1 implements Runnable
{
@Override
public void run() {
System.out.println("Exiting1");
}
public static void main(String[] args)
{
System.out.println(Thread.currentThread().getPriority());
Thread1 i=new Thread1();
Thread t=new Thread(i);
t.setPriority(8);
System.out.println(t.getPriority());
t.start();
System.out.println("Exiting");
}
}
这里我有一个疑问,我将线程的优先级设置为高于 main() 线程,那么为什么 main() 中的 sopln() 首先执行。我的线程应该首先执行,因此它应该首先执行 run() 方法的 sopln(),然后执行 main() 方法的 sopln。
最佳答案
参见Oracle docs : Background Information: Java Threads
A JVM is free to implement priorities in any way it chooses, including ignoring the value.
我的重点。
尽管本文档专门针对 Solaris 实现,但它应该适用于所有 JVM。
关于java - 为什么线程优先级不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43759205/