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(); } }
上一篇:
通过多线程提高代码的执行效率例子
下一篇:
SpringBoot中的异步线程池