I am create a scheduler and schedule a job with 2 sec delay. But before schedule specific jobs i want to check ScheduledExecutorService's pool size and queue size but i couldn't found any way for that. Can anyone suggest to me how can i check ScheduledExecutorService's pool size and queue size before schedule any job .
//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 interface doesn't allow to access the internal queue. You would have to cast the service to implementation class to get the size of the queue like this:
ScheduledExecutorService Scheduled_Executor_Service = Executors.newScheduledThreadPool(30);
if (Scheduled_Executor_Service instanceof ScheduledThreadPoolExecutor) {
ScheduledThreadPoolExecutor implementation = (ScheduledThreadPoolExecutor) Scheduled_Executor_Service;
int size = implementation.getQueue().size();
}