位置:首页 > Java技术 > java实例在线教程 > Java如何设置线程的优先级?

Java如何设置线程的优先级?

如何设置一个线程的优先级?

解决方法

下面的例子如何设置一个线程的优先级。

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.");
   }
}

结果

上面的代码示例将产生以下结果。

The priority has been set.