Java - ThreadGroup setMaxPriority() Method with Examples



Description

The Java ThreadGroup setMaxPriority() method sets the maximum priority of the group.Threads in the thread group that already have a higher priority are not affected.

Declaration

Following is the declaration for java.lang.ThreadGroup.setMaxPriority() method

public final void setMaxPriority(int priority)

Parameters

priority − This is the new priority of the thread group.

Return Value

This method does not return any value.

Exception

SecurityException − if the current thread cannot modify this thread group.

Example 1

The following example shows the usage of ThreadGroup setMaxPriority() method in case of a single ThreadGroup object. We've created a ThreadGroup object and assigned it a name. Then we've created two threads using the threadgroup object created earlier. Using setMaxPriority(), we're setting the maximum priority as Normal priority. Using getMaxPriority() method, we're getting the maximum priority of the thread group object.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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

Example 2

The following example shows the usage of ThreadGroup setMaxPriority() method in case of multiple ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Using setMaxPriority(), we're setting the maximum priority of child thread group as Normal priority(). Using getMaxPriority() method, we're printing max priorities of each threadgroup object.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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

Example 3

The following example shows the usage of ThreadGroup getMaxPriority() method in case of child and grandchild ThreadGroup objects. We've created a ThreadGroup object and assigned it a name. As next, we've created a child ThreadGroup object. Using setMaxPriority(), we're setting the maximum priority of parent thread group as Normal priority() Then we've created two threads using the child and grandchild threadgroup objects created earlier. Using getMaxPriority() method, we're printing max priorities of each threadgroup object. Here we can see that child and ancestor thread groups inherit the max priority from parent thread group object.

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.");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

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
Advertisements