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
广告