0
下面我有我的應用程序的方法,它的外觀做工精細..但林不知道是怎麼回事......未來相關澄清的Java
ArrayList arr = new ArrayList();
Collection<Future<?>> futures = new LinkedList<Future<?>>();
RestCallThread thread = null;
HttpHeaders header = getAuthHeader(authorization);
for (String ids : IDS) {
thread = new RestCallThread(ids, header);
futures.add(executor.submit(thread));
}
for (Future<?> future : futures) {
try {
arr.add(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
return arr;
在代碼中調用RESTAPI添加到ThreadPoolExecutor的和等待結果..
我的問題是由於該方法是基於線程如何不執行return語句,直到所有的API線程都完成...
任何機會return語句將返回空列表?
否,'future.get()'將阻塞,直到任務產生結果。如果每個任務都拋出異常,則只有空列表的機會 –