简体   繁体   English

具有start()方法的Java多线程优先级关系

[英]Java Multi Threading priority relation with start() method

My code and output are as below :- 我的代码和输出如下:

My question is that the priority of main thread and thread t4 is 9 and that of thread t is 5 then why line 1 to 4 (marked in output) is coming before line 5 ie why t.start() gets priority over t4.start() which will be executed by main thread which has priority of 9. 我的问题是,主线程和线程t4的优先级为9,线程t4的优先级为5,那么为什么行1至4(在输出中标记)在行5之前出现,即为什么t.start()的优先级高于t4.start ()将由优先级为9的主线程执行。

Main thread priority is 9 so first t4.start() should get executed but then why t.start() is getting executed first ? 主线程优先级为9,因此首先应该执行t4.start(),但是为什么首先要执行t.start()?

And if I reverse the order of calling start method ie if I call in order t4.start() and then t.start() then output is as expected. 如果我颠倒了调用start方法的顺序,即如果我按照t4.start()然后t.start()的顺序进行调用,则输出是预期的。

Output 输出量

5

5

Main Thread Priority :9

5

Calling MyRunnable2 from main....9

Child Class                                - line 1

Child Class                                - line 2

End of Child loop                                - line 3

Calling MyRunnable2 from MyThread                - line 4

MyRunnable2 Class....Main....9                   - line 5

MyRunnable2 Class....From MyThread run....5

CODE :- 代码:-

public class ThreadPriority 
{

    public static void main(String[] args) 
    {
        MyThread t = new MyThread();
        System.out.println(Thread.currentThread().getPriority());
        System.out.println(t.getPriority());
        t.setName("From MyThread");

        Thread.currentThread().setPriority(9);

        System.out.println("Main Thread Priority :" + Thread.currentThread().getPriority());
        System.out.println(t.getPriority());



        MyRunnable2 r4 = new MyRunnable2();
        Thread t4 = new Thread(r4,"Main");
        System.out.println("Calling MyRunnable2 from main"+"...."+t4.getPriority());

        t.start();
        t4.start();
    }

}


class MyRunnable2 implements Runnable
{
    public void run()
    {
        System.out.println("MyRunnable2 Class"+"...."+Thread.currentThread().getName()+"...."+Thread.currentThread().getPriority());

    }
}
class MyThread extends Thread
{
    public void run()
    {
        for (int i=0;i<2;i++)
            System.out.println("Child Class");

        System.out.println("End of Child loop");

        MyRunnable2 r2 = new MyRunnable2();
        Thread t2 = new Thread(r2,"From MyThread run");
        System.out.println("Calling MyRunnable2 from MyThread");
        t2.start();

    }

t4优先级更高,这并不意味着t4将首先完成,此外,jvm向OS询问线程,并将所有优先级设置在OS的较深层中,您只是设置了一个建议,它取决于很多事情!

The higher the integer, the higher the priority. 整数越大,优先级越高。 At any given time, when multiple threads are ready to be executed, the runtime system chooses the runnable thread with the highest priority for execution. 在任何给定时间,当准备好要执行多个线程时,运行时系统都会选择优先级最高的可运行线程来执行。 Only when that thread stops, yields, or becomes not runnable for some reason will a lower priority thread start executing. 仅当该线程停止,屈服或由于某种原因无法运行时,较低优先级的线程才会开始执行。

If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. 如果具有相同优先级的两个线程正在等待CPU,则调度程序选择其中一个以循环方式运行。

Rule of thumb : At any given time, the highest priority thread is running. 经验法则: 在任何给定时间,优先级最高的线程正在运行。 However, this is not a guaranteed. 但是,这不能保证。 The thread scheduler may choose to run a lower priority thread to avoid starvation. 线程调度程序可以选择运行一个较低优先级的线程以避免饥饿。 For this reason, use priority only to affect scheduling policy for efficiency purposes. 因此,出于效率考虑,仅将优先级用于影响调度策略。 Do not rely on thread priority for algorithm correctness. 不要依赖线程优先级来确保算法正确性。

I hope the above information clarifies your query. 希望以上信息能澄清您的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM