Thiết lập độ ưu tiên của Thread trong Java



Miêu tả vấn đề

Cách thiết lập độ ưu tiên thread trong Java?

Giải pháp

Ví dụ sau minh họa cách thiết lập độ ưu tiên thread trong Java bởi sử dụng phương thức setPriority(Thread.Priority).

public class Main {
   public static void main(String[] args) 
   throws Exception {
      Thread thread1 = new Thread(new TestThread(1));
      Thread thread2 = new Thread(new TestThread(2));
      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.");
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

The priority has been set.

thread_trong_java.jsp