如何在 Java 中設定執行緒的優先順序



問題描述

如何設定執行緒的優先順序?

解決方案

以下示例顯示瞭如何藉助以下設定執行緒的優先順序。

public class new_file {
   public static void main(String[] args) throws Exception {
      Thread thread1 = new Thread();
      Thread thread2 = new Thread();
      thread1.setPriority(Thread.MAX_PRIORITY);
      thread2.setPriority(Thread.MIN_PRIORITY);
      thread1.start();
      thread2.start();
      thread1.join();
      thread2.join();
      System.out.println("The priority has been set.");
   }
}

結果

上述程式碼示例將生成以下結果。

The priority has been set.
java_threading.htm
廣告
© . All rights reserved.