线程池CountDownLatch使用

​

    public JSONObject saveFolderPermission() {
        ...
        Set<String> oldUsernames = folderEnterprise.getUsernameSet();
        Set<String> oldOrgIds = folderEnterprise.getOrgIdsSet();
        Set<String> usernames = new HashSet<>();
        Set<String> orgIds = new HashSet<>();
        storageService.usernamesAndOrgIdsDeal(usernames, orgIds, sharedToUserArray, sharedToDeptArray);
        folderEnterprise.setFolderPermissions(JSONObject.parseObject(folderPermissions));
        folderEnterprise.setUsernameSet(usernames);
        folderEnterprise.setOrgIdsSet(orgIds);
        folderService.save(folderEnterprise);

        CountDownLatch countDownLatch = new CountDownLatch(1);
        AtomicInteger total = new AtomicInteger();
        ThreadLocalContext localContext = ThreadLocalContextHolder.getContext();
        MultiThread.pool.execute(() -> {
            ThreadLocalContext threadLocalContext = ThreadLocalContextHolder.getContext();
            MultiThread.multiThreadInit(threadLocalContext, localContext);
            Set<String> totalUsernamesSet = noticeService.getUserNameSet(Joiner.on(",").join(usernames), Joiner.on(",").join(orgIds));
            total.set(totalUsernamesSet.size());
            countDownLatch.countDown();
        });
        MultiThread.pool.execute(() -> {
            try {
                countDownLatch.await();
                ThreadLocalContext threadLocalContext = ThreadLocalContextHolder.getContext();
                MultiThread.multiThreadInit(threadLocalContext, localContext);
                if (folderEnterprise.getParent() == null) {
                    Set<String> resultUsernames = new HashSet<>();
                    Set<String> resultOrgIds = new HashSet<>();
                    //新增成员发送通知
                    resultUsernames.addAll(usernames);
                    resultUsernames.removeAll(oldUsernames);
                    for (String username : resultUsernames) {
                        for (int i = 0; i < sharedToUserArray.size(); i++) {
                            JSONObject sharedToUser = sharedToUserArray.getJSONObject(i);
                            String inUsername = sharedToUser.getString("username");
                            if (inUsername.equals(username)) {
                                noticeService.sendMessage(username, null, "", true, folderEnterprise.formatJson(), MessageTemplateEnum.levelOneFolderPermissionAdd, folderService.genLevelOneFolderPermissionAddParams(folderEnterprise, sharedToUser, total.get()));
                                break;
                            }
                        }
                    }
                    resultOrgIds.addAll(orgIds);
                    resultOrgIds.removeAll(oldOrgIds);
                    for (String orgId : resultOrgIds) {
                        for (int i = 0; i < sharedToDeptArray.size(); i++) {
                            JSONObject sharedToDept = sharedToDeptArray.getJSONObject(i);
                            String id = sharedToDept.getString("id");
                            if (id.equals(orgId)) {
                                noticeService.sendMessage(null, orgId, "", true, folderEnterprise.formatJson(), MessageTemplateEnum.levelOneFolderPermissionAdd, folderService.genLevelOneFolderPermissionAddParams(folderEnterprise, sharedToDept, total.get()));
                                break;
                            }
                        }
                    }
                    //修改成员权限发送通知

                    //删除成员权限发送通知

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return formatResult(null);
    }

​
public class MultiThread implements Runnable {
    public static ThreadPoolExecutor pool = new ThreadPoolExecutor(2, Integer.MAX_VALUE, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<>());

    @Override
    public void run() {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值