package thread;

public class Util {

  public static ThreadGroup getSystemThreadGroup() {
    ThreadGroup systemThreadGroup;
    ThreadGroup parentThreadGroup;
    systemThreadGroup = Thread.currentThread().getThreadGroup();
    while ((parentThreadGroup = systemThreadGroup.getParent()) != null)
      systemThreadGroup = parentThreadGroup;
    return systemThreadGroup;
  }


  public static Thread[] getThreads() {
    ThreadGroup stg = getSystemThreadGroup();
    Thread ta[] = new Thread[stg.activeCount()];
    stg.enumerate(ta, true);
    return ta;

  }

  public static ThreadGroup[] getThreadGroups() {
    ThreadGroup stg = getSystemThreadGroup();
    int nog = stg.activeGroupCount() + 1;
    ThreadGroup tga[] = new ThreadGroup[nog];
    stg.enumerate(tga);
    tga[tga.length - 1] = stg;
    return tga;
  }

  public static void print(Thread o[]) {
    for (int i = 0; i < o.length; i++)
      System.out.println(o[i] +
                         " isAlive=" +
                         o[i].isAlive() +
                         " name=" +
                         o[i].getName() +
                         " isDaemon=" +
                         o[i].isDaemon());
  }


  public static void setPriority(Thread ta[], int p) {
    for (int i = 0; i < ta.length; i++)
      ta[i].setPriority(p);
  }

  public static void setName(Thread ta[], String n) {
    for (int i = 0; i < ta.length; i++)
      ta[i].setName(n);
  }

  public static void setDaemon(Thread ta[], boolean on) {
    for (int i = 0; i < ta.length; i++)
      if (ta[i].isAlive() == true) {
        System.out.println(
            "The selected threads are in alive state ; so you can't change the daemon");
        return;
      } else
        ta[i].setDaemon(on);

  }
}