Thread: getThreadGroup() : Thread « java.lang « Java by API






Thread: getThreadGroup()

  
class Main {
  public static void main(String[] args) throws Exception {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    MyThread mt1 = new MyThread(tg, "first");
    MyThread mt2 = new MyThread(tg, "second");

    mt1.start();
    mt2.start();

    ThreadGroup parent = tg.getParent();
    Thread[] list = new Thread[parent.activeCount()];
    int count = parent.enumerate(list, true);
    String[] thdinfo = new String[count];
    for (int i = 0; i < count; i++)
      thdinfo[i] = list[i].toString();

    mt1.join();
    mt2.join();

    for (int i = 0; i < count; i++)
      System.out.println(thdinfo[i]);
  }
}

class MyThread extends Thread {
  MyThread(ThreadGroup tg, String name) {
    super(tg, name);
  }

  public void run() {
    for (char c = 'A'; c <= 'Z'; c++)
      System.out.println(c);

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
  }
}

   
    
  








Related examples in the same category

1.Thread.MAX_PRIORITY
2.Thread.NORM_PRIORITY
3.new Thread(Runnable target, String name)
4.Thread.activeCount()
5.Thread.currentThread()
6.Thread.dumpStack()
7.Thread.enumerate(Thread[] tarray)
8.Thread: getStackTrace()
9.Thread: getUncaughtExceptionHandler()
10.Thread: interrupt()
11.Thread: isAlive()
12.Thread: isDaemon()
13.Thread: join() (Using join() to wait for threads to finish)
14.Thread: run()
15.Thread: setDaemon(boolean b)
16.Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
17.Threah.sleep(long millis)
18.Thread: setDaemon(boolean on)
19.Thread: setName(String name)
20.Thread: setPriority(int newPriority)
21.Thread: setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
22.Thread: start
23.Thread: stop
24.implements UncaughtExceptionHandler