多线程中的Java线程优先级介绍和代码实例

2021年3月12日13:20:01 发表评论 587 次浏览

多线程环境中, 线程调度程序根据线程的优先级将处理器分配给线程。每当我们在Java中创建线程时, 始终会为其分配一些优先级。优先级可以由JVM在创建线程时给定, 也可以由程序员明确给定。

可接受的线程优先级值在1到10的范围内。Thread类中为优先级定义了3个静态变量。

公共静态int MIN_PRIORITY:

这是线程可以具有的最低优先级。值是1。

公共静态int NORM_PRIORITY:

如果未显式定义线程, 则这是线程的默认优先级。值是5。

公共静态整数MAX_PRIORITY:

这是线程的最大优先级。值是10。

获取和设置线程优先级

  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());
     }
}
Output:
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());
     }
}
Output:
Main thread priority : 6
t1 thread priority : 6

如果两个线程具有相同的优先级, 那么我们就无法预期哪个线程将首先执行。这取决于线程调度程序的算法(Round-Robin, 先到先服务等)

如果我们使用线程优先级进行线程调度, 则应始终牢记底层平台应支持基于线程优先级的调度。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: