Last active
December 28, 2022 09:33
-
-
Save sunzsh/80cf4eb0675385084f828dda4570baf5 to your computer and use it in GitHub Desktop.
Java仿Js实现Promise.all实现并行处理的能力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.util; | |
import java.util.concurrent.*; | |
public class Promise { | |
// 根据实际情况配置线程池 | |
static ExecutorService threadPool = new ThreadPoolExecutor(10, 40, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); | |
public static void allAwait(Runnable ...runnables) throws InterruptedException { | |
CountDownLatch checkFinish = new CountDownLatch(runnables.length); | |
for (Runnable runnable : runnables) { | |
threadPool.execute(()->{ | |
try { | |
runnable.run(); | |
} catch(Throwable e) { | |
e.printStackTrace(); | |
} finally { | |
checkFinish.countDown(); | |
} | |
}); | |
} | |
checkFinish.await(); | |
} | |
public static void all(Runnable ...runnables){ | |
for (Runnable runnable : runnables) { | |
threadPool.execute(runnable); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SpringSecurity默认在子线程中无法获取到用户信息,需要配置:
https://stackoverflow.com/questions/3467918/how-to-set-up-spring-security-securitycontextholder-strategy