我正在创建一个调度程序并安排一个延迟 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();
}
添加回答
举报
0/150
提交
取消