Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误与异常

Java 多线程

Java 同步

Java 网络编程

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java API 与框架

Java 类引用

Java 有用资源

Java - 线程优先级



Java 中的线程优先级

每个 Java 线程都有一个优先级,它可以帮助操作系统确定线程调度的顺序。您可以获取和设置线程的优先级。Thread 类提供用于处理线程优先级的方法和常量。

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

Thread 类的内置属性常量

Java 线程优先级范围在 MIN_PRIORITY(常量 1)和 MAX_PRIORITY(常量 10)之间。默认情况下,每个线程都赋予 NORM_PRIORITY(常量 5)优先级。

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

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

  • MAX_PRIORITY:指定线程可以具有的最高优先级。

线程优先级设置器和获取器方法

Java 中线程优先级的示例

在这个例子中,我们展示了一个简单的单线程程序,我们没有声明任何线程,并在程序执行中检查线程名称和优先级。

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

更多线程优先级的示例

示例 1

在这个例子中,我们创建了一个 ThreadDemo 类,它扩展了 Thread 类。我们创建了三个线程。每个线程都被分配了一个优先级。在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.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

示例 2

在这个例子中,我们创建了一个 ThreadDemo 类,它扩展了 Thread 类。我们创建了三个线程。由于我们没有设置任何优先级,因此每个线程都具有普通优先级。在 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
广告