为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉风的咖菲猫 2023-09-06 16:45:10
我正在创建一个调度程序并安排一个延迟 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 jobSCHEDULED_EXECUTOR_SERVICE.schedule(() -> {                    User user = new User();                    SCHEDULED_EXECUTOR_SERVICE.execute(user);                }, 2000, TimeUnit.MILLISECONDS);}
查看完整描述

1 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

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();


        }


查看完整回答
反对 回复 2023-09-06
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信