當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Thread setPriority()用法及代碼示例


線程類的 setPriority() 方法用於改變線程的優先級。每個線程都有一個優先級,由 1 到 10 之間的整數表示。

Thread 類提供了 3 個常量屬性:

  1. 公共靜態 int MIN_PRIORITY:它是線程的最大優先級。它的值為 1。
  2. 公共靜態 int NORM_PRIORITY:這是一個線程的正常優先級。它的值為 5。
  3. 公共靜態 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 setPriority() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。