Addev
Addev

Reputation: 32233

Parallelize network petitions in java

I have an Responseobject for representing an post response to a web service. Given this code:

Response resp1= Server.request(a);
Response resp2= Server.request(b);
Response resp3= Server.request(c);
if (resp1.isOk() && resp2.isOk() && resp3.isOk()){
    // Do stuff
}

Since the server is a little slow I'd like to parallelize the petitions in 3 threads. But I'm don't know how to make the main thread to wait for the result and retrieve the 3 responses.

Upvotes: 1

Views: 89

Answers (3)

Johan Sjöberg
Johan Sjöberg

Reputation: 49187

You have a few threading-based approaches.

  • The first is to expose a blocking call, e.g.,

    resp1.waitFoCompletion();

  • The second is to provide a polling mechanism (not very nice)

    while(resp1.isCompleted())

  • The third is to provide a callback which e.g., your main class can implement

    interface Callback() { void onCompletion(Result result); }

An important issue though is, why do this yourself when you can utilize e.g., Executors?

Upvotes: 1

user1359537
user1359537

Reputation: 26

You can't. You would have to spin off three separate threads, then make the primary thread poll the three threads and wait some amount of time if they aren't finished. If you know which of your services is slow, you can just have the primary thread call that one after launching separate threads for the others, but you still have to deal with the case that one of the others takes longer in a freakish circumstance.

A couple of comments, though: If this is really a loading issue, then spinning off extra threads probably won't help, and may actually hurt. This approach will only help if more than one of the services really is slow because they have a lot to do for this one call (and not because they are under load). PLUS they must be on separate hardware; if these are really sharing hardware, anyway, then you only get the illusion of running in parallel, because each one slows down the others, anyway, just in a multi-threaded way.

Finally, you shouldn't be spinning off new threads willy-nilly. They are a relatively expensive consumable resource. You should have a thread pool so that there are never more than N of them, and you have to be VERY CAREFUL that they never fall into a black hole. So all of the threads need to have timeouts, and the process must always clean up and return the threads to the pool in all the possible error cases.

Edit: The previous poster suggests using Executors and he also mentions callbacks. If you follow his link, you'll see that the default implementation of an Executor does not spin off a separate thread, it just waits for completion, so that isn't going to get you parallelism. However, the executor-with-a-callback pattern is a great one for implementing a threadpool and integrating the polling and waiting mechanism I mentioned above. Your executor deals with the threadpool and launching the tasks, then calls the callback. Your primary thread creates the executors with the tasks, and the callback is just something that sets a flag in the object in the primary thread. Now your primary thread just has to have a loop that looks for all the flags set and waits.

Upvotes: 1

sdabet
sdabet

Reputation: 18670

I guess you could do something like that to launch the calls in 3 threads and wait for their termination:

private Response resp1, resp2, resp3;

protected void doRequests() {
    Thread thread1 = new Thread() {
        public void run() {
            resp1= Server.request(a);           
        }
    };
    Thread thread2 = new Thread() {
        public void run() {
            resp1= Server.request(a);           
        }
    };
    Thread thread3 = new Thread() {
        public void run() {
            resp1= Server.request(a);           
        }
    };

    thread1.start();
    thread2.start();
    thread3.start();

    try { thread1.join(); } catch(InterruptedException e) { }
    try { thread2.join(); } catch(InterruptedException e) { }
    try { thread3.join(); } catch(InterruptedException e) { }
}

Upvotes: 1

Related Questions