当前位置:网站首页>2021-08-11 for循环结合多线程异步查询并收集结果
2021-08-11 for循环结合多线程异步查询并收集结果
2022-08-11 06:44:00 【yy1209357299】
JsonResult jsonResult = new JsonResult();
ExecutorService executorService = Executors.newCachedThreadPool();
HashMap<String,String> map = new HashMap<>();
for(int i=0;i<jsonArray.size();i++){
final int j = i;
executorService.execute(new Runnable() {
@Override
public void run() {
synchronized (this){
String name = jsonArray.getJSONObject(j).getString("1");
String id = jsonArray.getJSONObject(j).getString("2");
String url = null;
try {
url = getUrl(id);
} catch (Exception e) {
e.printStackTrace();
}
map.put(name,url);
}
}
});
}
executorService.shutdown();
while(true){
if(executorService.isTerminated()){
break;
}
}
if(map.size()>0){
jsonResult.setCode("200");
jsonResult.setResult(map);
}else {
jsonResult.setCode("300");
jsonResult.setResult("无资源");
}
边栏推荐
猜你喜欢
【LaTex-错误和异常】\verb ended by end of line.原因是因为闭合边界符没有在\verb命令所属行中出现;\verb命令的正确和错误用法、verbatim环境的用法
Douyin API interface
伦敦银规则有哪些?
daily sql - query for managers and elections with at least 5 subordinates
Daily sql-employee bonus filtering and answer rate ranking first
MySQL使用GROUP BY 分组查询时,SELECT 查询字段包含非分组字段
结合均线分析k线图的基本知识
每日sql:求好友申请通过率
每日sql -查询至少有5名下属的经理和选举
技术分享 | 实战演练接口自动化如何处理 Form 请求?
随机推荐
ROS 服务通信理论模型
concept noun
那些事情是用Unity开发项目应该一开始规划好的?如何避免后期酿成巨坑?
NFT 的价值从何而来
《猪猪1984》NFT 作品集将上线 The Sandbox 市场平台
伦敦银规则有哪些?
每日sql-统计各个专业人数(包括专业人数为0的)
Shell:三剑客之awk
Unity游戏排行榜的制作与优化
SQL sliding window
LeetCode刷题系列 -- 46. 全排列
Daily sql-seek the sum of successful investments in 2016
Edge provides label grouping functionality
详述 MIMIC护理人员信息表(十五)
Taobao API interface reference
博途PLC 1200/1500PLC ModbusTcp通信梯形图优化汇总(多服务器多从站轮询)
prometheus学习5altermanager
线程交替输出(你能想出几种方法)
How Unity programmers can improve their abilities
daily sql - query for managers and elections with at least 5 subordinates