当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。