public class casTest {
public static AtomicInteger atomicInteger;
public static CountDownLatch countDownLatch = new CountDownLatch(10000);
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
int count =0;
atomicInteger = new AtomicInteger(count);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 8,
10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),
new ThreadPoolExecutor.CallerRunsPolicy());
for (int i =0;i<10000;i++){
threadPoolExecutor.execute(new sale());
}
countDownLatch.await();
long end = System.currentTimeMillis();
System.out.println(atomicInteger.get());
System.out.println(end-start);
}
}
class sale implements Runnable{
public void run() {
casTest.atomicInteger.getAndIncrement();
casTest.countDownLatch.countDown();
}
}
Java 面试 :countdownlatch
最新推荐文章于 2024-03-21 19:39:16 发布