Java 简明教程

Java - Thread Priority

Priority of a Thread in Java

每个 Java 线程都有一个优先级,该优先级帮助 operating system 确定 threads 的调度顺序。你可以获取和设置 Thread 的优先级。Thread 类提供方法和常量来处理 Thread 的优先级。

具有较高优先级的线程对程序而言更为重要,并且应该在较低优先级的线程之前分配处理器时间。但是,线程优先级不能保证线程执行的顺序,并且很大程度上取决于平台。

Built-in Property Constants of Thread Class

Java 线程优先级介于 MIN_PRIORITY(常量为 1)和 MAX_PRIORITY(常量为 10)之间。默认情况下,每个线程都获得 NORM_PRIORITY(常量为 5)优先级。

  1. MIN_PRIORITY:指定线程可以具有的最低优先级。

  2. NORM_PRIORITY: 指定分配给线程的默认优先级。

  3. MAX_PRIORITY: 指定线程可能具有的最高优先级。

Thread Priority Setter and Getter Methods

  1. Thread.getPriority() Method :此方法用于获取线程的优先级。

  2. Thread.setPriority() Method :此方法用于设置线程的优先级,它接受优先级值并使用给定的优先级更新现有的优先级。

Example of Thread Priority in Java

在此示例中,我们展示了一个简单的单线程程序,我们不会在其中 declaring any thread 和检查程序执行中的线程名称和优先级。

package com.tutorialspoint;
public class TestThread {
   public void printName() {
      System.out.println("Thread Name: " + Thread.currentThread().getName());
      System.out.println("Thread Priority: " +Thread.currentThread().getPriority());
   }
   public static void main(String args[]) {
      TestThread thread = new TestThread();
      thread.printName();
   }
}
Thread Name: main
Thread Priority: 5

More Examples of Thread Priority

Example 1

在此示例中,我们创建了一个 ThreadDemo 类,它扩展了 Thread 类。我们创建了三个线程。每个线程都被分配了一个优先级。在 run() method 中,我们打印优先级并在输出中反映在线程执行中。

package com.tutorialspoint;
class ThreadDemo extends Thread {
   ThreadDemo( ) {
   }
   public void run() {
      System.out.println("Thread Name: " + Thread.currentThread().getName()
         + ", Thread Priority: " +Thread.currentThread().getPriority());
      for(int i = 4; i > 0; i--) {
         System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
      }
      try {
         Thread.sleep(50);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   public void start () {
      super.start();
   }
}
public class TestThread {
   public static void main(String args[]) {
      ThreadDemo thread1 = new ThreadDemo();
      ThreadDemo thread2 = new ThreadDemo();
      ThreadDemo thread3 = new ThreadDemo();
      thread1.setPriority(Thread.MAX_PRIORITY);
      thread2.setPriority(Thread.MIN_PRIORITY);
      thread3.setPriority(Thread.NORM_PRIORITY);
      thread1.start();
      thread2.start();
      thread3.start();
   }
}
Thread Name: Thread-2, Thread Priority: 5
Thread Name: Thread-1, Thread Priority: 1
Thread Name: Thread-0, Thread Priority: 10
Thread: Thread-1, 4
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-0, 4
Thread: Thread-1, 2
Thread: Thread-2, 3
Thread: Thread-0, 3
Thread: Thread-0, 2
Thread: Thread-0, 1
Thread: Thread-2, 2
Thread: Thread-2, 1
Thread: Thread-1, 1

Example 2

在此示例中,我们创建了一个扩展 Thread 类的 ThreadDemo 类。我们创建了三个线程。因为我们未设置任何优先级,所以每个线程都具有正常优先级。在 run() 方法中,我们打印优先级,并且在输出中,线程按任意顺序执行。

package com.tutorialspoint;
class ThreadDemo extends Thread {
   ThreadDemo( ) {
   }
   public void run() {
      System.out.println("Thread Name: " + Thread.currentThread().getName()
         + ", Thread Priority: " +Thread.currentThread().getPriority());
      for(int i = 4; i > 0; i--) {
         System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
      }
      try {
         Thread.sleep(50);
      } catch (InterruptedException e) {

         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   public void start () {
      super.start();
   }
}
public class TestThread {
   public static void main(String args[]) {
      ThreadDemo thread1 = new ThreadDemo();
      ThreadDemo thread2 = new ThreadDemo();
      ThreadDemo thread3 = new ThreadDemo();
      thread1.start();
      thread2.start();
      thread3.start();
   }
}
Thread Name: Thread-1, Thread Priority: 5
Thread Name: Thread-2, Thread Priority: 5
Thread Name: Thread-0, Thread Priority: 5
Thread: Thread-2, 4
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-0, 4
Thread: Thread-2, 2
Thread: Thread-1, 2
Thread: Thread-2, 1
Thread: Thread-0, 3
Thread: Thread-1, 1
Thread: Thread-0, 2
Thread: Thread-0, 1