Example usage for java.lang ThreadGroup enumerate

List of usage examples for java.lang ThreadGroup enumerate

Introduction

In this page you can find the example usage for java.lang ThreadGroup enumerate.

Prototype

public int enumerate(ThreadGroup list[]) 

Source Link

Document

Copies into the specified array references to every active subgroup in this thread group and its subgroups.

Usage

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("new Group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();//ww w .ja v  a 2  s.  co  m
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("Group");

    ThreadGroup newGroup = new ThreadGroup(group, "new group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();//from ww  w.  j av  a  2  s.  c  om
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    ThreadGroup tg = new ThreadGroup("My Group");

    MyThread thrd = new MyThread(tg, "MyThread #1");
    MyThread thrd2 = new MyThread(tg, "MyThread #2");
    MyThread thrd3 = new MyThread(tg, "MyThread #3");

    thrd.start();//from   www . ja  v  a 2 s  .  c  o  m
    thrd2.start();
    thrd3.start();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in thread group.");

    Thread thrds[] = new Thread[tg.activeCount()];
    tg.enumerate(thrds);
    for (Thread t : thrds)
        System.out.println(t.getName());

    thrd.myStop();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in tg.");
    tg.interrupt();
}

From source file:NewThread.java

public static void main(String args[]) {
    ThreadGroup groupA = new ThreadGroup("Group A");
    ThreadGroup groupB = new ThreadGroup("Group B");

    NewThread ob1 = new NewThread("One", groupA);
    NewThread ob2 = new NewThread("Two", groupA);
    NewThread ob3 = new NewThread("Three", groupB);
    NewThread ob4 = new NewThread("Four", groupB);

    groupA.list();//from  w  w  w. j ava 2s  .c om
    groupB.list();
    Thread tga[] = new Thread[groupA.activeCount()];
    groupA.enumerate(tga);
    for (int i = 0; i < tga.length; i++) {
        ((NewThread) tga[i]).mysuspend();
    }

    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }

    System.out.println("Resuming Group A");
    for (int i = 0; i < tga.length; i++) {
        ((NewThread) tga[i]).myresume();
    }

    try {
        ob1.join();
        ob2.join();
        ob3.join();
        ob4.join();
    } catch (Exception e) {
        System.out.println("Exception in Main thread");
    }
}

From source file:MyThread.java

public static void main(String args[]) {
    ThreadGroup groupA = new ThreadGroup("Group A");
    ThreadGroup groupB = new ThreadGroup("Group B");

    MyThread ob1 = new MyThread("One", groupA);
    MyThread ob2 = new MyThread("Two", groupA);
    MyThread ob3 = new MyThread("Three", groupB);
    MyThread ob4 = new MyThread("Four", groupB);

    System.out.println("\nHere is output from list():");
    groupA.list();/*from w ww .  j  av a  2s.c  om*/
    groupB.list();

    System.out.println("Suspending Group A");
    Thread tga[] = new Thread[groupA.activeCount()];
    groupA.enumerate(tga); // get threads in group
    for (int i = 0; i < tga.length; i++) {
        ((MyThread) tga[i]).suspendMe(); // suspend each thread
    }

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }

    System.out.println("Resuming Group A");
    for (int i = 0; i < tga.length; i++) {
        ((MyThread) tga[i]).resumeMe();
    }

    try {
        System.out.println("Waiting for threads to finish.");
        ob1.join();
        ob2.join();
        ob3.join();
        ob4.join();
    } catch (Exception e) {
        System.out.println("Exception in Main thread");
    }
    System.out.println("Main thread exiting.");
}

From source file:Main.java

public static void interruptThreadAndJoin(ThreadGroup threadGroup) {
    Thread[] threads = new Thread[threadGroup.activeCount()];
    threadGroup.enumerate(threads);

    for (Thread thread : threads) {
        interruptThreadAndJoin(thread);/*ww w  .j ava2s .com*/
    }
}

From source file:Main.java

public static void interruptThreadAndJoin(ThreadGroup threadGroup, int activeThreadsCount) {
    Thread[] threads = new Thread[activeThreadsCount];
    threadGroup.enumerate(threads);

    for (Thread thread : threads) {
        interruptThreadAndJoin(thread);/*from ww  w. j a  v  a  2s  .  c o m*/
    }
}

From source file:Main.java

public static Thread[] getThreadArray(ThreadGroup group) {
    Thread[] threads = new Thread[group.activeCount()];
    for (;;) {/*www.  j a v  a 2s. c o  m*/
        int l = group.enumerate(threads);
        if (l == threads.length)
            break;
        threads = new Thread[l];
    }
    return threads;
}

From source file:Main.java

public static List<String> namesOfActiveThreadsIn(ThreadGroup grp) {
    List<String> threadNames = new ArrayList<String>();
    Thread[] threads = new Thread[grp.activeCount() * 10];
    int count = grp.enumerate(threads);
    for (int i = 0; i < count; i++) {
        threadNames.add(threads[i].getName());
    }/*from w  w w.  j a  va2  s. c o  m*/
    return threadNames;
}

From source file:Mem.java

public static void dumpThreadInfo() {
    ThreadGroup g = Thread.currentThread().getThreadGroup();
    Thread[] t = new Thread[g.activeCount()];
    g.enumerate(t);
    System.err.println("Active threads in " + g);
    for (int i = 0; i < t.length; ++i)
        System.err.println(t[i]);
}