• Tags
  •         
  • www.breakyizhan.com
  •    

    在多线程环境中,线程调度程序根据线程的优先级将处理器分配给线程。每当我们在Java中创建一个线程时,它总是会为它分配一些优先级。优先级可以由JVM在创建线程时给出,也可以由程序员明确给出。
    线程优先级的可接受值在1到10的范围内。在Thread类中为优先级定义了3个静态变量。

    public static int MIN_PRIORITY:这是线程可以拥有的最低优先级。这个值是1.
    public static int NORM_PRIORITY:如果没有明确定义它,这是线程的默认优先级。值为5.
    public static int MAX_PRIORITY:这是线程的最大优先级。这个价值是10。

    获取并设置Java线程的优先级:

    1. public final int getPriority(): java.lang.Thread.getPriority()方法返回给定线程的优先级。
    2. public final void setPriority(int newPriority): java.lang.Thread.setPriority()方法将线程的优先级更改为值newPriority。如果参数newPriority的值超出最小(1)和最大(10)限制,则此方法抛出IllegalArgumentException。

    getPriority()和set的示例

    // Java program to demonstrate getPriority() and setPriority()
    import java.lang.*;
    
    class ThreadDemo extends Thread
    {
        public void run()
        {
            System.out.println("Inside run method");
        }
    
        public static void main(String[]args)
        {
            ThreadDemo t1 = new ThreadDemo();
            ThreadDemo t2 = new ThreadDemo();
            ThreadDemo t3 = new ThreadDemo();
    
            System.out.println("t1 thread priority : " +
                                  t1.getPriority()); // Default 5
            System.out.println("t2 thread priority : " +
                                  t2.getPriority()); // Default 5
            System.out.println("t3 thread priority : " +
                                  t3.getPriority()); // Default 5
    
            t1.setPriority(2);
            t2.setPriority(5);
            t3.setPriority(8);
    
            // t3.setPriority(21); will throw IllegalArgumentException
            System.out.println("t1 thread priority : " +
                                  t1.getPriority());  //2
            System.out.println("t2 thread priority : " +
                                  t2.getPriority()); //5
            System.out.println("t3 thread priority : " +
                                  t3.getPriority());//8
    
            // Main thread
            System.out.print(Thread.currentThread().getName());
            System.out.println("Main thread priority : "
                           + Thread.currentThread().getPriority());
    
            // Main thread priority is set to 10
            Thread.currentThread().setPriority(10);
            System.out.println("Main thread priority : "
                           + Thread.currentThread().getPriority());
        }
    }
    

    输出:

    t1 thread priority : 5
    t2 thread priority : 5
    t3 thread priority : 5
    t1 thread priority : 2
    t2 thread priority : 5
    t3 thread priority : 8
    Main thread priority : 5
    Main thread priority : 10

    注意:

    • 具有最高优先级的线程将在其他线程之前获得执行机会。假设存在3个线程t1,t2和t3,优先级为4,6和1.因此,线程t2将首先基于最大优先级6执行,之后执行t1,然后执行t3。
    • 主线程的默认优先级始终为5,稍后可以更改。所有其他线程的默认优先级取决于父线程的优先级。例:
      // Java program to demonstrat ethat a child thread
      // gets same priority as parent
      import java.lang.*;
      
      class ThreadDemo extends Thread
      {
          public void run()
          {
              System.out.println("Inside run method");
          }
      
          public static void main(String[]args)
          {
              // main thread priority is 6 now
              Thread.currentThread().setPriority(6);
      
              System.out.println("main thread priority : " +
                         Thread.currentThread().getPriority());
      
              ThreadDemo t1 = new ThreadDemo();
      
              // t1 thread is child of main thread
              // so t1 thread will also have priority 6.
              System.out.println("t1 thread priority : "
                                        + t1.getPriority());
          }
      }
      

      输出:

      Main thread priority : 6
      t1 thread priority : 6
    • 如果两个线程具有相同的优先级,那么我们不能指望哪个线程将首先执行。它取决于线程调度程序的算法(Round-Robin,First Come First Serve等)
    • 如果我们使用线程优先级进行线程调度,那么我们应该始终牢记底层平台应该基于线程优先级为调度提供支持。
     
    转载请保留页面地址:https://www.breakyizhan.com/java/4944.html