Konrad Höffner
Konrad Höffner

Reputation: 12207

How to concurrently run ScheduledThreadPoolExecutor

I have a program that periodically checks for new data from a web page and then downloads and converts it into another format. Because the download of one dataset consists mostly of waiting, the program can be sped up by processing one dataset while downloading another, thus I want to use multi threading. Unfortunately, ScheduledThreadpoolExecutor.scheduleAtFixedRate() blocks until one Runnable is finished, even if I set the executor constructor parameter corePoolSize to more than 1. As a workaround, I queued the same Runnable twice but is there a more elegant solution?

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Downloader
{
    public static void main(String[] args)
    {
        ScheduledThreadPoolExecutor e = new ScheduledThreadPoolExecutor(4);
        e.scheduleAtFixedRate(new Scheduler(), 0, 1, TimeUnit.SECONDS);
        e.scheduleAtFixedRate(new Scheduler(), 1, 1, TimeUnit.SECONDS);
    }
}

Upvotes: 0

Views: 66

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69329

Separate the task into two sub-tasks and schedule those separately on their own thread pools.

You could have X threads performing download tasks and queuing work onto a job queue and Y processing threads that dequeue work items and process them.

Upvotes: 2

Related Questions