java多綫程

ExecutorService threadPool = Executors.newFixedThreadPool(6);

List<AppDocFileUploadDto> files = fileUploadDto.getDocumentlist();

List<CompletableFuture<Void>> result = files.stream().map(fileDto ->
                    CompletableFuture.runAsync(
                            new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        String folderName = fileDto.getFileType();
                                        
                                        synchronized (folderName.intern()) {
                                            if (folderIds.isEmpty() || !folderIds.containsKey(folderName)) {
                                                WSFolder wsFolder = dmsExpService.createAppSubTypeFolder(sid, folderName, targetFolder.getId(), wsAttributes);
                                                folderIds.put(folderName, wsFolder.getId());
                                            }
                                        }
                                        String filePath = fileDto.getPath();
                                        MultipartFile file = null;
                                       
                                        dmsExpService.uploadWithAttributes(sid, file.getOriginalFilename(), folderIds.get(folderName), file, template.getId(),
                                                wsAttributes == null ? null : wsAttributes, loanEntry);

                                    } catch (Exception e) {
                                        logger.info("kafka send file to dms error , file : {} ,  {},", fileDto, e.getMessage());
                                    }
                                }
                            }
                    ,threadPool)).collect(Collectors.toList());

// 執行:

            result.stream().forEach(e-> {
                try {
                    e.get();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } catch (ExecutionException ex) {
                    ex.printStackTrace();
                }
            });

  

 

二: 把一个数组分成多个:

ListUtils.partition(users, 600)

@Resource
private Executor asyncTaskExecutor;
 private void splitBatchSend(String content, List<UserEntity> users) {
        /*
            发送频率:通过 RESTful API 单个应用每分钟最多可发送 6000 条消息,每次最多可向 600 人发送。
            例如,一次向 600 人发消息,视为 600 条消息。
         */
        List<List<UserEntity>> partitions = ListUtils.partition(users, 600);
        partitions.forEach(partition -> CompletableFuture.runAsync(() -> {
            redisScoreUtil.incSendHXCount((long) partition.size());
            batchMessages(content, partition);
            Set emChatUsernames = partition.stream()
                    .map(UserEntity::getEmchatUsername)
                    .collect(Collectors.toSet());
            HXUtils.sendMessage(Constant.ADMIN_SYS_MESSAGE, HXIMTypeEnum.users.name(), emChatUsernames, content);
        }, asyncTaskExecutor));
    }

 

posted @ 2022-05-20 15:47  zhoudingzhao  阅读(14)  评论(0编辑  收藏  举报