框架有没有多线程,使用线程池处理呢?

Blade 已结 2 1905
asit
asit 2020-06-29 20:35
2条回答
  •  asit
    asit (楼主)
    2020-06-30 10:02

    是不是这样使用的呢?谢谢!

        /**
         * 异步发送
         *
         * @param id
         */
        @Async("taskExecutor")
        public void sendAgencyMessage(String id) {
            log.info("异步通知");
            // 参数传递用id,使用更小的字节放入缓存队列
            messageJobService.sendAgencyMessage(null,id);
        }


    回答: 2020-06-30 10:02

    另一种用法:

    @Autowired
    private TaskExecutor taskExecutor;
     
    public ResultVO findHandlingRecordByAssociationId(Integer associationId) throws InterruptedException{
        Map<String, Object> map = new HashMap<>(2);
       //线程计数器(等待所有线程执行完统一返回)
        CountDownLatch countDownLatch = new CountDownLatch(10);
        taskExecutor.execute(() -> {
            try {
                //service调用
                map.put("HandlingRecord", legalLitigationService.findHandlingRecordByAssociationId(associationId));
            }finally {
                countDownLatch.countDown();
            }
        });
        taskExecutor.execute(() -> {
            try {
                map.put("CaseBasic", legalLitigationService.findCaseDetailsById(associationId));
            }finally {
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        return ResultVO.putSuccess(map);
    }


提交回复