Java - ThreadGroup setMaxPriority() 方法



描述

Java ThreadGroup setMaxPriority() 方法設定組的最大優先順序。執行緒組中已經具有更高優先順序的執行緒不受影響。

宣告

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

public final void setMaxPriority(int priority)

引數

priority − 這是執行緒組的新優先順序。

返回值

此方法不返回值。

異常

SecurityException − 如果當前執行緒無法修改此執行緒組。

在 ThreadGroup 物件中設定最大優先順序的示例

以下示例演示了在單個 ThreadGroup 物件的情況下使用 ThreadGroup setMaxPriority() 方法。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。然後,我們使用前面建立的 threadgroup 物件建立了兩個執行緒。使用 setMaxPriority(),我們將最大優先順序設定為普通優先順序。使用 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");
         threadGroup.setMaxPriority(Thread.NORM_PRIORITY);
         // 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 =5
Thread-0 finished executing.
Thread-1 finished executing.

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

以下示例演示了在多個 ThreadGroup 物件的情況下使用 ThreadGroup setMaxPriority() 方法。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。使用 setMaxPriority(),我們將子執行緒組的最大優先順序設定為普通優先順序()。使用 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");
         cThreadGroup.setMaxPriority(Thread.NORM_PRIORITY);
         // 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 =5
Thread-0 finished executing.
Thread-1 finished executing.

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

以下示例演示了在子執行緒組和孫執行緒組物件的情況下使用 ThreadGroup getMaxPriority() 方法。我們建立了一個 ThreadGroup 物件併為其指定了一個名稱。接下來,我們建立了一個子 ThreadGroup 物件。使用 setMaxPriority(),我們將父執行緒組的最大優先順序設定為普通優先順序()。然後,我們使用前面建立的子執行緒組和孫執行緒組物件建立了兩個執行緒。使用 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");
		 pThreadGroup.setMaxPriority(Thread.NORM_PRIORITY);
         // 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 =5
Maximum priority of cThreadGroup =5
Maximum priority of gThreadGroup =5
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
廣告