1

前のタスクが完了するのを待たずに、特定の間隔でタスクを実行できるようにする ScheduledExecutor のバリアントを探しています。

以下のコードでは、5000ms 間隔で 12 の異なる出力行があります。

実行間隔が 50 ミリ秒でスレッド プール サイズが 10 の場合、最初の 550 ミリ秒に 10 行の出力行があり、スレッドが解放されて再利用できるようになるまで一時停止するソリューションを探しています。

    ScheduledExecutorService pollingExecutorService = Executors.newScheduledThreadPool(10);

    final Runnable pollingTask = new Runnable() {
        public void run() {
            System.out.println("running poller " + DateTime.now().toString());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            System.out.println("5000 passed");
        }
    };

    ScheduledFuture<?> pollingHandler =
            pollingExecutorService.scheduleWithFixedDelay(pollingTask,
                    0,
                    50,
                    TimeUnit.MILLISECONDS);

    //wait secondsIdleBeforeShutdown seconds
    try {
        Thread.sleep(1000*60);
    } catch (InterruptedException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
4

1 に答える 1