我想要测量有多少个线程同时执行一段代码。目前,我正在(滥用)使用信号量来实现这一点,是否有更好的方法?
final int MAX_THREADS = Integer.MAX_VALUE;
Semaphore s = new Semaphore(MAX_THREADS);
s.acquire(); // start of section
// do some computations
// track how many threads are running the section
trackThreads( (MAX_THREADS - s.availablePermits()) );
s.release(); // end of section