Java 面试 :countdownlatch

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();
    }
}
经验分享 程序员 微信小程序 职场和发展