天天看點

集合線程執行

/**
 * @author YUFEI
 * @Date 2022/8/10 20:04
 * @Version 1.0
 *  公共執行
 */
public class CommonConsumers {
     public static void consumers(List<Consumer<Long>> consumers,String tenant,Long deptPk){
         ThreadPoolTaskExecutor executor = SpringUtils.getBean("threadPoolTaskExecutor");
         consumers.stream().map(consumer->
                 CompletableFuture.runAsync(()->{
                     ThreadLocalUtil.set(tenant);
                     consumer.accept(deptPk);
                     ThreadLocalUtil.remove();
                 },executor)
         ).map(CompletableFuture::join).collect(Collectors.toList());
     }


}