Java Thread setPriority()方法

Thread類的setPriority()方法用於設置更改線程的優先順序。每個線程都有一個優先順序,由110之間的整數表示。

Thread類提供3個常量屬性:

  • public static int MIN_PRIORITY:它是線程的最大優先順序,它的值是1
  • public static int NORM_PRIORITY:這是線程的普通優先順序,它的值是5
  • public static int MAX_PRIORITY:它是線程的最小優先順序,它的值是10

還可以將線程的優先順序設置在110之間。此優先順序稱為自定義優先順序或用戶定義的優先順序。

語法

  • a:將此線程設置為優先順序。

異常

  • IllegalArgumentException:如果優先順序不在MIN_PRIORITYMAX_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)

上一篇: Java Runtime類 下一篇:無