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類 下一篇:無