當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ThreadGroup getMaxPriority()用法及代碼示例


ThreadGroup類getMaxPriority()方法

  • getMaxPriority() 方法在 java.lang 包中可用。
  • getMaxPriority() 方法用於返回該線程組中存在的線程的最高優先級,並且該線程組中沒有線程的優先級必須高於最大優先級。
  • getMaxPriority() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • getMaxPriority() 方法在返回該線程組的最大優先級時不拋出異常。

用法:

    public final int  getMaxPriority();

參數:

  • 它不接受任何參數。

返回值:

這個方法的返回類型是int– 返回該線程組中存在的線程的最大優先級。

例:

// Java program to demonstrate the example 
// of int getMaxPriority() method of ThreadGroup 

public class GetMaxPriority implements Runnable {
    public static void main(String[] args) {
        GetMaxPriority max_pri = new GetMaxPriority();
        max_pri.maxPriority();
    }

    public void maxPriority() {
        try {
            // Create two thread group and the named are base
            // and derived

            ThreadGroup base = new ThreadGroup("Base ThreadGroup");
            ThreadGroup derived = new ThreadGroup(base, "Derived ThreadGroup");

            // create two threads
            Thread th1 = new Thread(base, this);
            Thread th2 = new Thread(derived, this);

            // By using getName() method is to retrieve the
            // name of the thread th1
            System.out.println(th1.getName() + " " + "begins.....");

            // By using start() method is to start its execution 
            // of thread th1
            th1.start();

            // By using getName() method is to retrieve the
            // name of the thread th2
            System.out.println(th2.getName() + " " + "begins.....");


            // By using start() method is to start its execution 
            // of thread th2
            th2.start();

            System.out.println();

            // By using getMaxPriority() method is to return the
            // maximum priority of the base and derived thread group

            System.out.println("base.getMaxPriority():" + " " + base.getMaxPriority());
            System.out.println("derived.getMaxPriority():" + " " + derived.getMaxPriority());

            // By using join() method is to wait the current
            // thread till complete execution of another
            // thread

            th1.join();
            th2.join();

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    // Override run()
    public void run() {
        for (int k = 0; k < 100; ++k)
            ++k;
        System.out.println(Thread.currentThread().getName() + " " + "ends.....");
    }
}

輸出

Thread-0 begins.....
Thread-1 begins.....

Thread-0 ends.....
Thread-1 ends.....
base.getMaxPriority():10
derived.getMaxPriority():10


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ThreadGroup getMaxPriority() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。