如何从 ScheduledExecutorService 获取队列和池大小

How to get queue and pool size from ScheduledExecutorService

我正在创建一个调度程序并安排一个延迟 2 秒的作业。但在安排特定作业之前,我想检查 ScheduledExecutorService 的池大小和队列大小,但我找不到任何方法。谁能建议我如何在安排任何作业之前检查 ScheduledExecutorService 的池大小和队列大小。

   //here i have created ScheduledExecutorService 
private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE;


static {
     //initialized  ScheduledExecutorService with 30 pool size
    Scheduled_Executor_Service = (ThreadPoolExecutor) Executors.newScheduledThreadPool(30);
}


public void addTaskTOSchedule(){

  //Here i want to check poolsize and queue size of SCHEDULED_EXECUTOR_SERVICE if 
  //ScheduledExecutorService already used more then specific number thread then i will not schedule job

SCHEDULED_EXECUTOR_SERVICE.schedule(() -> {
                    User user = new User();
                    SCHEDULED_EXECUTOR_SERVICE.execute(user);
                }, 2000, TimeUnit.MILLISECONDS);
}

ScheduledExecutorService 接口不允许访问内部队列。您必须将服务转换为实现 class 才能像这样获得队列的大小:

ScheduledExecutorService Scheduled_Executor_Service = Executors.newScheduledThreadPool(30);
        if (Scheduled_Executor_Service instanceof ScheduledThreadPoolExecutor) {
            ScheduledThreadPoolExecutor implementation = (ScheduledThreadPoolExecutor) Scheduled_Executor_Service;
            int size = implementation.getQueue().size();

        }