简体   繁体   中英

How to speed up processing multiple HTTP request

I am working on a Java server which needs to send multiple HTTP get requests to another server and process the responses and wrap them up. Currently I am using synchronous way to do this like in Java :

for(Request request: requestList){
    Response response = client.send(request);
}

This works, but it takes a long time if the requestList 's size is big, since it is doing it sequentially: send a request, wait for the response and then send next.

I am wondering, is there a way to speed this up? I am thinking if using multiple thread to send multiple request will do the trick? Or maybe not, since the total response time is fixed for a certain amount of requests.

Any idea or explanation, why or why not this will work will be helpful. I am using Java .

If you use Java 8, you can try lambdas for parallel stream.

requestList.parallelStream().forEach(....);

But it is not good idea to use them, when the requests depends on one another. Also if your app is used by multiple users, I don't know how your server will handle that many requests.

Other solutions is to use threads. But you should limit the number of threads(thread pool) for every loop(user). Of course this depends on how many resources you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM