Java Program to Set Max Priority of ThreadGroup

You are currently viewing Java Program to Set Max Priority of ThreadGroup

Java program to set max priority of ThreadGroup

// Java program to set the max priority 
// of ThreadGroup

class MyThread extends Thread {
  MyThread(String threadname, ThreadGroup tg) {
    super(tg, threadname);
    start();
  }
  public void run() {
    try {
      Thread.sleep(100);
      System.out.println(Thread.currentThread().getName() + " is finished");
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

public class Main {
  public static void main(String[] args) {
    try {
      ThreadGroup group = new ThreadGroup("Parent thread");

      MyThread t1 = new MyThread("Child Thread1", group);
      System.out.println(t1.getName() + " is started");

      MyThread t2 = new MyThread("Child Thread2", group);
      System.out.println(t2.getName() + " is started");

      t1.join();
      t2.join();

      System.out.println("Maximum priority of Thread Group: " + group.getMaxPriority());
      group.setMaxPriority(5);
      System.out.println("Maximum priority of Thread Group: " + group.getMaxPriority());
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output

Child Thread1 is started
Child Thread2 is started
Child Thread1 is finished
Child Thread2 is finished
Maximum priority of Thread Group: 10
Maximum priority of Thread Group: 5

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply