亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 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 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號