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类 下一篇:无