简体   繁体   English

连续运行ExecutorService任务

[英]Run ExecutorService tasks continuously

I want to use method newWorkStealingPool() to get thread and run them continuously every 1 sec . 我想使用newWorkStealingPool()方法获取线程并每1 sec连续运行一次。 Using the following sample code : 使用以下示例代码:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

        Runnable task = () -> System.out.println("Scheduling: " + System.currentTimeMillis());

        int initialDelay = 0;
        int period = 1;
        executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);

I can run task continuously but I want to use the method newWorkStealingPool() to get threads. 我可以连续运行任务,但是我想使用newWorkStealingPool()方法获取线程。 Using the following code: 使用以下代码:

ScheduledExecutorService executor = (ScheduledExecutorService)Executors.newWorkStealingPool();

        Runnable task = () -> System.out.println("Scheduling: " + System.currentTimeMillis());

        int initialDelay = 0;
        int period = 1;
        executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);

I got the error: 我得到了错误:

java.util.concurrent.ForkJoinPool cannot be cast to java.util.concurrent.ScheduledExecutorService

Using ExecutorService object it's possible to use newWorkStealingPool() but I don't know if is there any way to run ExecutorService object continuously like what object ScheduledExecutorService provides? 使用ExecutorService对象可以使用newWorkStealingPool()但我不知道是否有任何方法可以像ScheduledExecutorService提供的对象那样连续运行ExecutorService对象?

I think this can be achieved with creating ScheduledExecutorService and ForkJoinPool . 我认为可以通过创建ScheduledExecutorServiceForkJoinPool ScheduledExecutorService will be used to submit tasks to ForkJoinPool at specified intervals. ScheduledExecutorService将用于以指定的时间间隔将任务提交到ForkJoinPool And ForkJoinPool will execute these tasks. ForkJoinPool将执行这些任务。

    ForkJoinPool executor = (ForkJoinPool) Executors.newWorkStealingPool();
    // this will be only used for submitting tasks, so one thread is enough
    ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    Runnable task = () -> System.out.println("Scheduling: " + System.currentTimeMillis());
    int initialDelay = 0;
    int period = 1;
    scheduledExecutor.scheduleAtFixedRate(()->executor.submit(task), initialDelay, period, TimeUnit.SECONDS);

The Executors.newWorkStealingPool() produces a ForkJoinPool . Executors.newWorkStealingPool()生成一个ForkJoinPool The ForkJoinPool class does not implement the ScheduledExecutorService interface so you cannot cast it to a ScheduledExecutorService . ForkJoinPool类未实现ScheduledExecutorService接口,因此无法将其强制转换为ScheduledExecutorService

Furthermore the ForkJoinPool and ScheduledExecutorService are fundamentally different thread pools. 此外, ForkJoinPoolScheduledExecutorService是根本不同的线程池。 If you need to schedule a task to execute once every second stick with a ScheduledExecutorService , since it is suitable for your use case. 如果您需要安排一个任务使用ScheduledExecutorService每秒钟执行一次,因为它适合您的用例。 ForkJoinPools are intended to use in cases where you have many small units of work divided among many threads, not for when you want to regularly execute something. ForkJoinPools旨在用于在许多线程中分配了许多小工作单元的情况下,而不是在您要定期执行某事时使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM