我想找到一個簡單的方法來發送http請求併發到不同的web服務。每個請求是完全相互獨立的。併發http請求到獨立的web服務
目前,我實現這個樣子(只是一個簡化,不注重設計)
讓我們說我有一個清單查詢;
public class Service {
private List<HttpClient> httpClients; // one for each web service
public List<QueryResult> doQueries(List<Query> queries) {
ExecutorService service = Executors.... ;
List<Callable<QueryResult>> .... ;
for (Query q : queries) {
Future<> .....
}
service.invokeAll(...) ;
***// what should i do from here ?
// how should i wait all those tasks to finish ?***
}
}
我的問題是具體的。 我該如何等待?
請分享完整的代碼。將請求提交給「ExecutorService」後,你在哪裏收集「Future」? –
嗨@akhil_mittal。這是我的問題,我如何從該代碼開始?我如何收集每個線程結果? – justatester
['Fork and Join'](https://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html)? –