如何从 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);

}


拉风的咖菲猫
浏览 87回答 1
1回答

红糖糍粑

ScheduledExecutorService 接口不允许访问内部队列。您必须将服务转换为实现类才能获取队列的大小,如下所示:ScheduledExecutorService Scheduled_Executor_Service = Executors.newScheduledThreadPool(30);        if (Scheduled_Executor_Service instanceof ScheduledThreadPoolExecutor) {            ScheduledThreadPoolExecutor implementation = (ScheduledThreadPoolExecutor) Scheduled_Executor_Service;            int size = implementation.getQueue().size();        }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java