java 并发 简单使用

文章目录

    • 概要
    • 代码

概要

java 并发 简单使用

代码

    public static final ExecutorService EXECUTOR_GENERAL = new ThreadPoolExecutor(100, 1000,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10000));
        int size = 1000;
        List<UserService> userServices = new ArrayList<>(size);
        List<CarService> carServices = new ArrayList<>(size);
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i = 0; i < size; i++) {
            EXECUTOR_GENERAL.execute(() -> {
                try {
                    System.out.println("cur thread " + Thread.currentThread().getName());
                    UserService userService = applicationContext.getBean(UserService.class);
                    userServices.add(userService);
                    CarService carService = applicationContext.getBean(CarService.class);
                    carServices.add(carService);
                } catch (Exception ignore) {
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        boolean await = countDownLatch.await(5, TimeUnit.SECONDS);

你可能感兴趣的:(java,java,开发语言)