Java - ThreadGroup getMaxPriority() 方法



描述

Java ThreadGroup getMaxPriority() 方法返回該執行緒組的最大優先順序。屬於該組的執行緒不能具有高於最大優先順序的優先順序。

宣告

以下是 java.lang.ThreadGroup.getMaxPriority() 方法的宣告

public final int getMaxPriority()

引數

返回值

此方法返回該執行緒組中執行緒可以具有的最大優先順序。

異常

獲取 ThreadGroup 物件的最大優先順序示例

以下示例顯示了在單個 ThreadGroup 物件的情況下使用 ThreadGroup getMaxPriority() 方法的情況。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 getMaxPriority() 方法,我們獲取了執行緒組物件的所有最大優先順序。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // returns the maximum priority of thread group
         int i = threadGroup.getMaxPriority();
         System.out.println("Maximum priority of threadGroup =" + i);

         // block until the other threads finish
         t1.join();
         t2.join();        


      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Maximum priority of threadGroup =10
Thread-0 finished executing.
Thread-1 finished executing.

在多個 ThreadGroup 物件中獲取 ThreadGroup 的最大優先順序示例

以下示例顯示了在多個 ThreadGroup 物件的情況下使用 ThreadGroup getMaxPriority() 方法的情況。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的執行緒組物件建立了兩個執行緒。使用 getMaxPriority() 方法,我們列印每個執行緒組物件的最大優先順序。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();

         // returns the maximum priority of thread group
         int i = pThreadGroup.getMaxPriority();
         System.out.println("Maximum priority of pThreadGroup =" + i);

         i = cThreadGroup.getMaxPriority();
         System.out.println("Maximum priority of cThreadGroup =" + i);
         // block until the other threads finish
         t1.join();
         t2.join();

      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Maximum priority of pThreadGroup =10
Maximum priority of cThreadGroup =10
Thread-1 finished executing.
Thread-0 finished executing.

在子/孫 ThreadGroup 物件中獲取 ThreadGroup 的最大優先順序示例

以下示例顯示了在子執行緒組和孫執行緒組物件的情況下使用 ThreadGroup getMaxPriority() 方法的情況。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。使用 getMaxPriority() 方法,我們列印每個執行緒組物件的最大優先順序。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a grandchild ThreadGroup for parent ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // returns the maximum priority of thread group
         int i = pThreadGroup.getMaxPriority();
         System.out.println("Maximum priority of pThreadGroup =" + i);

         i = cThreadGroup.getMaxPriority();
         System.out.println("Maximum priority of cThreadGroup =" + i);

         i = gThreadGroup.getMaxPriority();
         System.out.println("Maximum priority of gThreadGroup =" + i);

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Starting Thread-0...
Starting Thread-1...
Maximum priority of pThreadGroup =10
Maximum priority of cThreadGroup =10
Maximum priority of gThreadGroup =10
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
廣告