Java教程

Java getPriority()方法

线程类的 getPriority()方法用于检查线程的优先级。创建线程时,会为其分配一些优先级。线程的优先级既可以由JVM分配,也可以由程序员在创建线程时明确分配。
线程的优先级在1到10的范围内。线程的默认优先级是5。

语法

public final int getPriority()

返回

它返回线程的优先级。

示例

public class JavaGetPriorityExp extends Thread
{
    public void run()
    {
        System.out.println("running thread name is:"+Thread.currentThread().getName());
    }
    public static void main(String args[])
    {
        // creating two threads
        JavaGetPriorityExp t1 = new JavaGetPriorityExp();
        JavaGetPriorityExp t2 = new JavaGetPriorityExp();
        // print the default priority value of thread
        System.out.println("t1 thread priority : " + t1.getPriority());
        System.out.println("t2 thread priority : " + t2.getPriority());
        // this will call the run() method
        t1.start();
        t2.start();
    }
}
输出:
t1 thread priority : 5
t2 thread priority : 5
running thread name is:Thread-0
running thread name is:Thread-1

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