Parallelizing HTTP Requests in Java

52 views Asked by At

I developed a GUI, which connects to a server and fetches data on push of a button. The GUI is made in Swing.

For the requests, I have a specific class, which I cannot change. This class implements Callable.

To not block the GUI, I issue the requests in the background using SwingWorkers.

Now, for the requests I am dealing with, it is necessary to first fetch some data (in x requests) and fill a list with the fetched values. Then, x further requests need to be issued using the data of this list.

I tried to to this by using Executors.newFixedThreadPool with FutureTasks. Something like this:

ArrayList<GetRequestTask> tasks = new ArrayList<GetRequestTask>();
while(!tasks.isEmpty()) {
    for (Iterator<GetRequestTask> it = tasks.iterator(); it.hasNext();) {
    GetRequestTask task = it.next();
    if(task.isDone()) {
        response = new JSONObject(task.getResponse());
        reponses.add(respons);
            it.remove();
    }
    }
    if(!tasks.isEmpty()) Thread.sleep(100);
}

However, the tasks list never seems to be fully empty, because I will get stuck in the while loop. Does anybody know why this could be?

Or alternatively, do you have an idea how to do this in an easier or more efficient way? Thanks!

0

There are 0 answers