java parrallel for,Java 8 parallel forEach进度指示

java parrallel for,Java 8 parallel forEach进度指示ForperformancereasonIwouldliketouseaforEachloopofaparallelLambdastreaminordertoprocessaninstanceofaCollectioninJava.AsthisrunsinabackgroundServiceIwouldliketouse…

大家好,又见面了,我是你们的朋友全栈君。

java parrallel for,Java 8 parallel forEach进度指示

For performance reason I would like to use a forEach loop of a parallel Lambda stream in order to process an instance of a Collection in Java. As this runs in a background Service I would like to use the updateProgress(double,double) method in order to inform the user about the current progress.

In order to indicate the current progress I need a certain progress indicator in form of a Integer counter. However, this is not possible as I can only access final variables within the Lambda expression.

Code example see below, Collection is only a place holder for any possible instance of a Collection:

int progress = 0;

Collection.parallelStream().forEach(signer -> {

progress++;

updateProgress(progress, Collection.size());

});

I’m aware that I can solve this problem by using a simple for-loop. However, for performance reason it would nice to solve it in this way.

Does anybody know a more or less neat solution to this?

解决方案

As proposed by markspace, using an AtomicInteger is a good solution:

AtomicInteger progress = new AtomicInteger();

Collection.parallelStream().forEach(signer -> {

progress.incrementAndGet();

// do some other useful work

});

I would not use the runLater() variant as your goal is a high performance, and if many parallel threads will generte JavaFX ‘runLater’ tasks, you will again create a bottleneck…

For the same reason I would NOT call an update to the ProgressBar each time, but use a seaparte JavaFX Timeline to update the progress bar in regular intervals independently from the processing threads.

Here is a full code comparing sequential and parallel processing with ProgressBar. If you remove the sleep(1) and set the number of items to 10 million it will still work concurrently and efficiently…

public class ParallelProgress extends Application {

static class ParallelProgressBar extends ProgressBar {

AtomicInteger myDoneCount = new AtomicInteger();

int myTotalCount;

Timeline myWhatcher = new Timeline(new KeyFrame(Duration.millis(10), e -> update()));

public void update() {

setProgress(1.0*myDoneCount.get()/myTotalCount);

if (myDoneCount.get() >= myTotalCount) {

myWhatcher.stop();

myTotalCount = 0;

}

}

public boolean isRunning() { return myTotalCount > 0; }

public void start(int totalCount) {

myDoneCount.set(0);

myTotalCount = totalCount;

setProgress(0.0);

myWhatcher.setCycleCount(Timeline.INDEFINITE);

myWhatcher.play();

}

public void add(int n) {

myDoneCount.addAndGet(n);

}

}

HBox testParallel(HBox box) {

ArrayList myTexts = new ArrayList();

for (int i = 1; i < 10000; i++) {

myTexts.add(“At “+System.nanoTime()+” ns”);

}

Button runp = new Button(“parallel”);

Button runs = new Button(“sequential”);

ParallelProgressBar progress = new ParallelProgressBar();

Label result = new Label(“-“);

runp.setOnAction(e -> {

if (progress.isRunning()) return;

result.setText(“…”);

progress.start(myTexts.size());

new Thread() {

public void run() {

long ms = System.currentTimeMillis();

myTexts.parallelStream().forEach(text -> {

progress.add(1);

try { Thread.sleep(1);} catch (Exception e1) { }

});

Platform.runLater(() -> result.setText(“”+(System.currentTimeMillis()-ms)+” ms”));

}

}.start();

});

runs.setOnAction(e -> {

if (progress.isRunning()) return;

result.setText(“…”);

progress.start(myTexts.size());

new Thread() {

public void run() {

final long ms = System.currentTimeMillis();

myTexts.forEach(text -> {

progress.add(1);

try { Thread.sleep(1);} catch (Exception e1) { }

});

Platform.runLater(() -> result.setText(“”+(System.currentTimeMillis()-ms)+” ms”));

}

}.start();

});

box.getChildren().addAll(runp, runs, progress, result);

return box;

}

@Override

public void start(Stage primaryStage) throws Exception {

primaryStage.setTitle(“ProgressBar’s”);

HBox box = new HBox();

Scene scene = new Scene(box,400,80,Color.WHITE);

primaryStage.setScene(scene);

testParallel(box);

primaryStage.show();

}

public static void main(String[] args) { launch(args); }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161787.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 一个不容错过的Spring Cloud实战项目!

    mall-swarm作为mall项目的SpringCloud版本,目前已更新至最新代码,新增了权限管理功能。mall项目中的代码将一直保持最新,mall-swarm每过一段时间将从ma…

    2022年4月1日
    57
  • 删除数组中某个指定元素的值_如何删除数组中的元素

    删除数组中某个指定元素的值_如何删除数组中的元素首先可以给JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引,代码为:Array.prototype.indexOf=function(val){for(vari=0;i<this.length;i++){if(this[i]==val)returni;}return-1;};然后使用通过得到这个元素的索引,使用js数组自己固有的函数去删除这个元素:Array.prototype.remove=function(va

    2022年8月11日
    8
  • Jenkins(2)docker容器中安装python3「建议收藏」

    Jenkins(2)docker容器中安装python3「建议收藏」前言使用docker安装jenkins环境,jenkins构建的workspace目录默认是在容器里面构建的,如果我们想执行python3的代码,需进容器内部安装python3的环境。进jenki

    2022年7月29日
    5
  • navicat15 激活码【注册码】

    navicat15 激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    44
  • GB50174-2017机房标准_机房建设标准规范 2018

    GB50174-2017机房标准_机房建设标准规范 2018中华人民共和国国家标准电子计算机机房设计规范GB50174-93中华人民共和国国家标准电子计算机机房设计规范GB50174-93DesignCodeforElectronicComputerRo批准部门:中华人民共和国建设部1993年2月17日1993年9月1日实施第一章总则第1.0.1条为了使电子计算机机房设计确保电子计算机系统稳定可靠运行及保障机房工作人员有良好的工作环境,做到…

    2022年10月2日
    2
  • CSS改变鼠标样式(图片)

    CSS改变鼠标样式(图片)下面就来介绍下步骤方法:首页把鼠标图标格式转换成.ico格式,大小为32*32转换格式网址为:https://www.easyicon.net/covert/然后在CSS样式中增加代码:*{cursor:url(../images/shubiao.ico),auto;}大功告成啦~~~说明:图片大小最好是32*32的大小Css中的cursor属性不仅仅需…

    2022年5月31日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号