我的 service 想要开启一个线程做计算,
所以我用了
Class MyService() { SimpleAsyncTaskExecutor theExecutor = new SimpleAsyncTaskExecutor(); theExecutor.setConcurrencyLimit(1); Runnable indexAll = () -> { // do job } public voi doJob() { theExecutor.execute(indexAll); } }
但这样的设置是,比如我调用 service.doJob() 10 次,他最终会执行 10 次,一个一个来。我的要求是一旦已经有 job 在执行了,这时候调用 service.doJob() 不会放到队列里去,也就是直接忽略。
请问有这样的设置吗?或者 Spring 有提供任何相关的工具?
谢谢
![]() | 1 hello2060 OP 看来有个 bounded thread pool 可以设置超过指定数量以后丢弃,我来试试看 |
![]() | 2 oaix 2021-05-20 09:34:44 +08:00 new ThreadPoolExecutor( 1, // core 1, // max 0, // alive TimeUnit.SECONDS, new SynchronousQueue<>(), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { // ignore } } ); |
![]() | 3 reeco 2021-05-20 10:05:51 +08:00 via iPhone 加个锁不就好了吗 |
4 ailaoli 2021-05-20 10:13:22 +08:00 CountDownLatch ? |