ScheduledExecutorService multiple threads in parallel

I am interested in using ScheduledExecutorService to create multiple threads for tasks if the task has not completed yet. For example, I need to process a file every 0.5 s. The first task starts processing the file, after 0.5s, if the first thread is not completed, the second thread is generated and begins to process the second file, and so on. This can be done something like this:

  ScheduledExecutorService executor = Executors.newScheduledThreadPool(4) while (!executor.isShutdown()) { executor.execute(task); try { Thread.sleep(500); } catch (InterruptedException e) { // handle } } 

Now my question is: why can't I do this with executor.scheduleAtFixedRate ?

What I get is that the first task takes longer, the second task starts immediately after the first completion, but the new thread does not start, even if the artist has a thread pool. executor.scheduleWithFixedDelay understandable - it performs tasks with the same time interval between them, and it does not matter how much time is required to complete the task. Therefore, perhaps I misunderstood the purpose of the ScheduledExecutorService .

Maybe I should look at another artist? Or just use the code I posted here? Any thoughts?

+6
source share
2 answers

I solved the problem by running nested anonymous execution on every scheduled execution:

 final ScheduledExecutorService service = Executors.newScheduledThreadPool(POOL_SIZE); final Runnable command = new SlowRunnable(); service.scheduleAtFixedRate( new Runnable() { @Override public void run() { service.execute(command); } }, 0, 1, TimeUnit.SECONDS); 

In this example, there will be 1 thread executing a fast command on each interval, so it will be sure to end when the next interval expires. The remaining POOL_SIZE-1 threads will run SlowRunnable () in parallel, which may take longer than the duration of one interval.

Please note that although I like this solution, since it minimizes the code and reuses the same ScheduledExecutorService, it should be properly configured and may not be used in any context: if SlowRunnable is so slow that before POOL_SIZE the tasks are executed together during There will be no threads running the scheduled task.

Also, if you set the interval to 1 TimeUnit.NANOSECONDS, it will probably become too slow and the execution of the main runnable.

+5
source

One of the scheduleAtFixedRate methods is what you are looking for. It starts a task in a thread from the pool at a given interval, even if previous tasks are not finished yet. If you run out of threads for processing, adjust the pool size limits as described in ThreadPoolExecutor Docs .

0
source

Source: https://habr.com/ru/post/893694/


All Articles