Created
April 2, 2015 10:12
-
-
Save salyh/5ad9dc3a36b5bda27e14 to your computer and use it in GitHub Desktop.
thread group enumeration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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