我基本上有一个字符串列表(大约 20 到 60 个字符串),我想发送一个 post 请求(并获取 json 格式的结果,并将响应中的特定值附加到新列表)列表中的项目。我想将 ThreadPoolExecutor 与特定数量的工作人员一起使用。
我尝试了一些事情,但不幸的是我做不到。我可以一次执行一个请求,但这效率不高并且需要很长时间。
我在 python 中有这段代码,它完全实现了我想要做的事情,但不幸的是我无法在 Java 中重现它。
#This function makes a single request using one string from the list
def getname(id):
url = "https://api-public-service.battledash.co/fortnite/cosmetics/search/id?q=" + id
with requests.session() as ss:
l = ss.get(url).json()
#we return a value from the response
return(l['readableName'])
def getnamelist(ids):
result = []
ids = deque(ids)
#Turning the list into a dict and back to list in order to remove duplicated items
ids = list(dict.fromkeys(ids))
#max workers is set to 10
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
#running the getname function which sends a single request and return a name from id
results = executor.map(getname,ids)
#appending all results into a list
for it in tuple(results):
result.append(it)
return(result)
最佳答案
注意:由于您没有指定 Java 版本,所以我的答案针对的是 Java >= 8。
假设您有以下线程安全函数:public SomeHttpResponseType PerformGetRequest(String url)
:
public List<SomeHttpResponseType> performGetRequests(List<String> urls) {
return urls.stream().parallelStream()
.map(this::performGetRequest)
.collect(Collectors.toList())
}
这使用默认的 ForkJoinPool。如果您想指定自己的线程池,请尝试以下操作:
ForkJoinPool forkJoinPool = null;
try {
forkJoinPool = new ForkJoinPool(parallelism);
forkJoinPool.submit(() ->
urls.stream().parallelStream()
.map(this::performGetRequest)
.collect(Collectors.toList())
).get()
} catch(InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
if (forkJoinPool != null) {
forkJoinPool.shutdown(); //always remember to shutdown the pool
}
}
(https://www.codementor.io/nitinpuri/controlling-parallelism-of-java-8-collection-streams-umex0qbt1的改编版本)
关于java - 向 android 中的 URL 列表发出多个 get 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56299738/