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

Blade 已结 2 1868
asit
asit 剑圣 2020-06-29 20:35
2条回答
  •  smallchill
    smallchill (最佳回答者)
    2020-06-29 22:19

    image.png.

    作者追问:2020-06-29 22:19

    谢谢!请问如何调用呢?在说明文档里面好像没有找到?能指点一下吗?

    0 讨论(1)
  • 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:07

    另一种用法:

    @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);
    }


    0 讨论(0)
提交回复