Java Thread getPriority()方法

Thread类的getPriority()方法用于检查线程的优先级。 当创建一个线程时,它会为它分配一些优先级。线程的优先级可以由JVM或程序员在创建线程时明确指定。

线程的优先级在110的范围内。线程的默认优先级为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

上一篇: Java Runtime类 下一篇:无