Комбинация ThreadPoolExecutor и ArrayBlockingQueue не работает - PullRequest
0 голосов
/ 18 марта 2019

Я использую ThreadPoolExecutor весны.Может кто-нибудь сказать мне, как я могу отклонить дополнительные задачи в TaskQueue, когда он полон?По сути, я пытаюсь ограничить количество задач в очереди.Даже если в очереди выполняется задача, очередь всегда продолжает принимать новую задачу.Он не генерирует RejectedExecutionException

Я использую следующий код для создания ThreadPoolExecutor при запуске приложения:

@SpringBootApplication
@EnableSwagger2
@EnableFeignClients
@EnableAsync
@EnableScheduling
public class MyApplication {

  public static void main(String[] args) throws Throwable {
    SpringApplication.run(TpsRegistrationApplication.class, args);
  }

  @Bean(name = "threadPoolExecutor")
  public ThreadPoolExecutor threadPoolExecutor() {
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1, true);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.MILLISECONDS,
        blockingQueue, new RejectedExecutionHandler() {

          @Override
          public void rejectedExecution(Runnable r, java.util.concurrent.ThreadPoolExecutor executor) {
            // TODO Auto-generated method stub
            throw new TooManyRequestsException();
          }
        });
    // Let start all core threads initially
    executor.prestartAllCoreThreads();
    return executor;
  }

}

Затем я использую threadPoolExecutor для выполнения Runnable:

@Autowired
  private ThreadPoolExecutor threadPoolExecutor;

  @RequestMapping(value = "/test/executor", method = RequestMethod.POST)
  public @ResponseBody ResponseEntity<Void> testExecutor(@Valid @RequestBody String name) {
    threadPoolExecutor.execute(new RunnableTask(name));
    return new ResponseEntity<>(HttpStatus.OK);
  }
...