Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java多线程优先级:为什么在本例中,有时t1出现在t2完成之前,即使t2具有更高的优先级?_Java_Multithreading_Threadpool_Thread Priority - Fatal编程技术网

Java多线程优先级:为什么在本例中,有时t1出现在t2完成之前,即使t2具有更高的优先级?

Java多线程优先级:为什么在本例中,有时t1出现在t2完成之前,即使t2具有更高的优先级?,java,multithreading,threadpool,thread-priority,Java,Multithreading,Threadpool,Thread Priority,例如: class MyThread extends Thread{ public MyThread(String name) { super(name); } public void run(){ for (int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() +"("+Thread.currentThread().getPri

例如:

class MyThread extends Thread{  
public MyThread(String name) {
    super(name);
}

public void run(){
    for (int i=0; i<5; i++) {
        System.out.println(Thread.currentThread().getName()
                +"("+Thread.currentThread().getPriority()+ ")"
                +", loop "+i);
    }
} 
}; 

public class Demo {  
    public static void main(String[] args) {  

    System.out.println(Thread.currentThread().getName()
            +"("+Thread.currentThread().getPriority()+ ")");

    Thread t1=new MyThread("t1");    // t1
    Thread t2=new MyThread("t2");    // t2
    t1.setPriority(1);                // t1 priority 1
    t2.setPriority(10);                //t2 priority 10
    t1.start();                        // start t1
    t2.start();                        // start t2
}  
}
有时我的输出如下所示:

//Output1
main(5)
t2(10), loop 0
t2(10), loop 1
t2(10), loop 2
t2(10), loop 3
t2(10), loop 4
t1(1), loop 0
t1(1), loop 1
t1(1), loop 2
t1(1), loop 3
t1(1), loop 4
//Output2
main(5)
t1(1), loop 0
t1(1), loop 1
t1(1), loop 2
t1(1), loop 3
t1(1), loop 4
t2(10), loop 0
t2(10), loop 1
t2(10), loop 2
t2(10), loop 3
t2(10), loop 4 
在其他一些情况下,我有t1首先开始的输出,t2在t1完成所有输出之前开始

我认为,
output1
作为
我们如何理解这个例子背后的原因?

正如你在文章中提到的:

“优先级较高的线程优先于优先级较低的线程执行。”


这确实意味着,优先级较高的线程比优先级较低的线程执行的可能性更高。这并不意味着优先级较高的线程总是先执行/先完成。实际的线程处理取决于操作系统(java只使用其运行的操作系统提供的线程库)。

优先级较低的线程首先启动,因此在某些情况下,它可能在优先级较高的线程启动之前就完成了。5次迭代没有那么多。
在我的(Windows)计算机上,如果我将迭代次数替换为100,则始终首先选择优先级较高的线程。

我关于线程优先级的说明:

  • 您可以使用它来提高性能
  • 优先级取决于操作系统(Windows有7个,Linux忽略它们)
  • 不要以正确工作取决于优先级的方式设计应用程序
  • 如果某些具有高优先级的线程从未停用,则具有较低优先级的线程永远无法运行

如果你正在收集经验法则,那么你可能会从中获益,这里还有一个:释放资源的线程应该以比想要获得资源的线程更高的优先级运行。对于否决者:请解释为什么你否决了答案我没有否决你,但有一种可能性是,答案相当笼统,不涉及OPcode@user140547是的,有可能