Java Thread getPriority()方法

java.lang.Thread.getPriority() 方法返回当前线程的优先级。

1 语法

public final int getPriority()

2 参数

3 返回值

此方法返回该线程的优先级。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.getPriority()方法的例子
 */
import java.lang.*;

public class Demo {

    public static void main(String args[]) {
        new ThreadDemo();
    }
}

class ThreadDemo implements Runnable {

    Thread t;
    ThreadDemo() {

        // thread created
        t = new Thread(this, "Admin Thread");

        // set thread priority
        t.setPriority(1);

        // print thread created
        System.out.println("thread  = " + t);

        // this will call run() function
        t.start();
    }

    public void run() {
        // returns this thread's priority.
        System.out.println("Thread priority = " + t.getPriority());
    }
}

输出结果为:

thread  = Thread[Admin Thread,1,main]
Thread priority = 1

热门文章

优秀文章