Java Data Type Tutorial - Java ThreadGroup .parentOf (ThreadGroup g)








Syntax

ThreadGroup.parentOf(ThreadGroup g) has the following syntax.

public final boolean parentOf(ThreadGroup g)

Example

In the following code shows how to use ThreadGroup.parentOf(ThreadGroup g) method.

public class Main {
  public static void main(String[] args) {
    ThreadGroupDemo tg = new ThreadGroupDemo();
  }/*from  w ww . j  a v  a  2  s . c  om*/
}

class ThreadGroupDemo implements Runnable {

  public ThreadGroupDemo() {
    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    // determine which ThreadGroup is parent
    boolean isParent = pGroup.parentOf(cGroup);
    System.out.println(pGroup.getName() + " is the parent of "
        + cGroup.getName() + "? " + isParent);

    isParent = cGroup.parentOf(pGroup);
    System.out.println(cGroup.getName() + " is the parent of "
        + pGroup.getName() + "? " + isParent);

  }

  public void run() {
    System.out.println(Thread.currentThread().getName()
        + " finished executing.");
  }
}

The code above generates the following result.