[英]Java JSONArray and Parallelization of iteration
我有一段要并行化的代码,因为它们是独立的操作-
List<StockQuote> topGainers = new ArrayList<StockQuote>();
JSONObject jsonObject = (JSONObject)new JSONParser().parse(
new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
JSONArray dataArray = (JSONArray) jsonObject.get("data");
//Parallelize the for loop
for(int iter=0;iter<dataArray.size();iter++) {
JSONObject temp = (JSONObject) dataArray.get(iter);
System.out.println(temp.get("symbol"));
//getQuote function has network calls.
//Serial implementation makes it take too much time
topGainers.add(this.getQuote((String)temp.get("symbol")));
}
return topGainers;
这段代码如何并行化? ArrayList及其添加操作线程安全吗?
我试过了-
int size = dataArray.size();
ForkJoinPool forkJoinPool = new ForkJoinPool(size);
forkJoinPool.submit(() ->
IntStream.range(1, size).parallel().filter( (IntPredicate)i -> {
JSONObject temp = (JSONObject) dataArray.get(i);
System.out.println(temp.get("symbol"));
try {
System.out.println(temp.get("symbol"));
topGainers.add(this.getQuote((String)temp.get("symbol")));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;}));
我得到的topGainers数组为空
ArrayList
不是Synchronized
集合。 因此, ArrayList#add
不是线程安全的。 使用CopyOnWriteArrayList
代替它是线程安全的。 Java-8为我们提供了一些出色的功能,其中之一是集合上的parallelStream
。 因此,您可以利用它。 但是,如果它给您带来更多好处,您总是可以自由创建自己的线程池并执行任务。
以下示例可能对您有帮助
CopyOnWriteArrayList<StockQuote> topGainers = new CopyOnWriteArrayList<>();
dataArray.parallelStream().forEach(e->{
JSONObject temp = (JSONObject) dataArray.get(iter);
System.out.println(temp.get("symbol"));
//getQuote function has network calls.
//Serial implementation makes it take too much time
topGainers.add(this.getQuote((String)temp.get("symbol")));
});
您的云还使用Collections#synchronizedList
。
注释1对问题1的回答: dataArray.parallelStream().forEach
将并行遍历该列表。
通过评论回答您的问题2:
dataArray.parallelStream().forEach(e->{
JSONObject temp = (JSONObject) dataArray.get(iter);
System.out.println(temp.get("symbol"));
//getQuote function has network calls.
//Serial implementation makes it take too much time
if (some condition here) {
throw new YourException("Your exception messge");
}
topGainers.add(this.getQuote((String)temp.get("symbol")));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.