Java教程

Java setPriority()方法

线程类的 setPriority()方法用于更改线程的优先级。每个线程都有一个优先级,该优先级由1到10之间的整数表示。
线程类提供3个常量属性:
公共静态整数MIN_PRIORITY: 它是线程的最大优先级。它的值为1。 公共静态int NORM_PRIORITY: 这是线程的正常优先级。它的值为5。 公共静态整数MAX_PRIORITY: 这是线程的最低优先级。它的值为10。
我们还可以将线程的优先级设置为1到10。此优先级称为自定义优先级或用户定义的优先级。

语法

public final void setPriority(int a)

参数

a : 将此线程设置为优先级。

返回

它不返回任何值。

异常

IllegalArgumentException: 如果优先级不是,则抛出此异常。
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)

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4