Skip to content

Instantly share code, notes, and snippets.

@salyh
Created April 2, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salyh/5ad9dc3a36b5bda27e14 to your computer and use it in GitHub Desktop.
Save salyh/5ad9dc3a36b5bda27e14 to your computer and use it in GitHub Desktop.
thread group enumeration
package testthread;
import java.util.concurrent.CountDownLatch;
public class Main {
public static void main(String[] args) throws InterruptedException {
final int size = 3;
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
latch.countDown();
Thread.sleep(1000*1000);
} catch (InterruptedException e) {
}
}
});
t.setDaemon(true);
t.start();
latch.await();
ThreadGroup group = Thread.currentThread().getThreadGroup();
System.out.println(group.activeCount());
Thread[] threads = new Thread[size];
int enumerate = group.enumerate(threads);
System.out.println(enumerate);
System.out.println(threads.length);
}
//size=0
//2
//0
//0
//size=1
//2
//1
//1
//size=2
//2
//2
//2
//size=3
//2
//2
//3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment