Using an executor is fine, you may want to increase the size of the ThreadPool though to have more concurrent threads performing your requests.
Use a CoutnDownLatch initialised with numRequests
which sits waiting for all the threads to complete.
util.Request
must call latch.countDown()
in its run
method
The code would look like this (handwritten - not tested)
ExecutorService executor = Executors.newFixedThreadPool(n);
final CountDownLatch latch = new CountDownLatch(numRequests);
for (int i=0; i < numRequests; i++) {
executor.submit(new util.Request(new URL(url), latch));
}
latch.await(someValue, TimeUnit.SECONDS)
` Edit
Re-implement util.Request
doing something like
public class Request implements Callable<Response> {
final private URL url;
final private CountDownLatch latch;
public Request(URL url, CountDownLatch latch) {
this.url = url;
this.latch = latch;
}
@Override
public Response call() throws Exception {
try {
return new Response(url.openStream());
}
catch (Exception e) {
//do something useful
}
finally {
latch.countDown();
}
}
}
You may want to consume the stream of you response before you countDown the latch to verify that you get what you expect as a response from your server.