線程類的 setPriority() 方法用於改變線程的優先級。每個線程都有一個優先級,由 1 到 10 之間的整數表示。
Thread 類提供了 3 個常量屬性:
- 公共靜態 int MIN_PRIORITY:它是線程的最大優先級。它的值為 1。
- 公共靜態 int NORM_PRIORITY:這是一個線程的正常優先級。它的值為 5。
- 公共靜態 int MAX_PRIORITY:它是線程的最低優先級。它的值為 10。
我們還可以將線程的優先級設置在 1 到 10 之間。這個優先級被稱為自定義優先級或用戶定義的優先級。
用法
public final void setPriority(int a)
參數
a: 優先設置這個線程。
返回
它不返回任何值。
異常
IllegalArgumentException:如果優先級不在 MIN_PRIORITY 到 MAX_PRIORITY 範圍內,則拋出此異常。
SecurityException:如果當前線程無法修改此線程,則拋出此異常。
示例 1:最大優先級線程
public class JavaSetPriorityExp1 extends Thread
{
public void run()
{
System.out.println("Priority of thread is:"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp1 t1=new JavaSetPriorityExp1();
// print the maximum priority of this thread
t1.setPriority(Thread.MAX_PRIORITY);
// call the run() method
t1.start();
}
}
輸出:
Priority of thread is:10
示例 2:最小優先級線程
public class JavaSetPriorityExp2 extends Thread
{
public void run()
{
System.out.println("Priority of thread is:"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp2 t1=new JavaSetPriorityExp2();
// print the minimum priority of this thread
t1.setPriority(Thread.MIN_PRIORITY);
// This will call the run() method
t1.start();
}
}
輸出:
Priority of thread is:1
示例 3:普通優先級線程
public class JavaSetPriorityExp3 extends Thread
{
public void run()
{
System.out.println("Priority of thread is:"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp3 t1=new JavaSetPriorityExp3();
// print the normal priority of this thread
t1.setPriority(Thread.NORM_PRIORITY);
// starting the thread
t1.start();
}
}
輸出:
Priority of thread is:5
示例 4:用戶定義優先線程
public class JavaSetPriorityExp4 extends Thread
{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp4 t1=new JavaSetPriorityExp4();
JavaSetPriorityExp4 t2=new JavaSetPriorityExp4();
// set the priority
t1.setPriority(4);
t2.setPriority(7);
// print the user defined priority
System.out.println("Priority of thread t1 is:" + t1.getPriority()); //4
System.out.println("Priority of thread t2 is:" + t2.getPriority()); //7
// this will call the run() method
t1.start();
}
}
輸出:
Priority of thread t1 is:4 Priority of thread t2 is:7 running...
示例 5:當優先級大於 10 時
public class JavaSetPriorityExp5 extends Thread
{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp5 t1=new JavaSetPriorityExp5();
JavaSetPriorityExp5 t2=new JavaSetPriorityExp5();
// set the priority
t1.setPriority(12);
t2.setPriority(7);
// print exception because priority of t1 is greater than 10
System.out.println("Priority of thread t1 is:" + t1.getPriority());
System.out.println("Priority of thread t2 is:" + t2.getPriority());
// call the run() method
t1.start();
}
}
輸出:
Exception in thread "main" java.lang.IllegalArgumentException at java.lang.Thread.setPriority(Thread.java:1089) at JavaSetPriorityExp5.main(JavaSetPriorityExp5.java:13)
相關用法
- Java Thread setDefaultUncaughtExceptionHandler()用法及代碼示例
- Java Thread setContextClassLoader()用法及代碼示例
- Java Thread setDaemon()用法及代碼示例
- Java Thread setName()用法及代碼示例
- Java Thread suspend()用法及代碼示例
- Java Thread sleep()用法及代碼示例
- Java Thread stop()用法及代碼示例
- Java Thread start()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
- Java Thread destroy()用法及代碼示例
- Java Thread holdLock()用法及代碼示例
- Java Thread getContextClassLoader()用法及代碼示例
- Java Thread getThreadGroup()用法及代碼示例
- Java Thread isInterrupted()用法及代碼示例
- Java Thread enumerate()用法及代碼示例
- Java Thread notify()用法及代碼示例
- Java Thread resume()用法及代碼示例
- Java Thread activeCount()用法及代碼示例
- Java Thread isDaemon()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Thread setPriority() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。