Thread
類的setPriority()
方法用於設定更改執行緒的優先順序。每個執行緒都有一個優先順序,由1
到10
之間的整數表示。
Thread
類提供3
個常數屬性:
public static int MIN_PRIORITY
:它是執行緒的最大優先順序,它的值是1
。public static int NORM_PRIORITY
:這是執行緒的普通優先順序,它的值是5
。public static int MAX_PRIORITY
:它是執行緒的最小優先順序,它的值是10
。還可以將執行緒的優先順序設定在1
到10
之間。此優先順序稱為自定義優先順序或使用者定義的優先順序。
語法
a
:將此執行緒設定為優先順序。異常
IllegalArgumentException
:如果優先順序不在MIN_PRIORITY
到MAX_PRIORITY
的範圍內,則丟擲此異常。SecurityException
:如果當前執行緒無法修改此執行緒,則丟擲此異常。範例一 :最大優先順序執行緒
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
範例二 :最小優先順序執行緒
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
範例三 :正常優先順序執行緒
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
範例四 :使用者定義優先順序執行緒
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...
範例五 :當優先順序大於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)