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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 什么是redis

    什么是redis

    2021年10月18日
    52
  • SpringBoot跨域设置(CORS)「建议收藏」

    SpringBoot跨域设置(CORS)「建议收藏」目录什么是跨域跨域资源共享(CORS)1.简单请求2.非简单请求SpringBoot设置CORS1.配置过滤器CorsFilter2.实现接口WebMvcConfigurer3.使用注解@CrossOrigin什么是跨域请求url的协议、域名、端口三者有任意一个不同即为跨域。跨域问题是因为浏览器的同源策略的限制而产生的。同源:请求url的协议、域名、端口三者都相同即为同源(同一个域)。同源策略:同源策略(Sameoriginpolicy)是一种约定,他是浏览器最核心也最基本的安全

    2022年6月18日
    28
  • redis 乐观锁_用了乐观锁还需要事物吗

    redis 乐观锁_用了乐观锁还需要事物吗文章目录GeospatialHyperloglogBitmapsRedis事务悲观锁和乐观锁JedisSpringboot继承RedisGeospatial存储地理位置的数据结构应用场景朋友的定位,附近的人,打车距离计算Geospatial底层使用的是Zset127.0.0.1:6379> geoadd city 116.23 40.22 beijing 添加一个数据127.0.0.1:6379> geoadd city 121.47 31.23 shanghai 118.77

    2022年8月8日
    4
  • UML图之四——活动图

    UML图之四——活动图点击打开链接活动图是一种流程图,用来描述活动的序列,从一个活动到另一个活动的控制流。活动图的作用:描述用例,描述类的操作。 活动图的构成必要组成元素:1、活动:命令的执行,活动的进行。图符表示:2、状态:开始状态,结束状态。图符表示: 3、转移:活动之间,活动与状态之间的转换。4、判断:对一个动作或者状态进行判断,然后选择要执行的下一步操作。

    2022年5月5日
    42
  • 系统错误号:0x8007005[通俗易懂]

    系统错误号:0x8007005通常这个错误代码是错误的权限导致的,所以只要改变系统的安全设置就行了。下载这个文件SubInACL(SubInACL.exe)http://www.microsoft.com/downloads/details.aspx?FamilyID=e8ba3e56-d8fe-4a91-93cf-ed6985e3927b&displaylang=en安装这

    2022年4月7日
    289
  • 晶体管 开关电路_pnp开关电路

    晶体管 开关电路_pnp开关电路数字电路有两种,流控和压控晶体管作为开关时,就是用了它的“截止”和“饱和”两个状态。CPU中的逻辑开关电路,原理上不是电流来控制的,而是(不准确的说是)电压来控制的,准确的说叫电场来控制的,只要电场在,零电流也能保持打开或关闭,这种晶体管开关叫MOSFET(金属氧化物半导体场效晶体管Metal-Oxide-SemiconductorField-EffectTransistor),特别的…

    2025年12月6日
    5

发表回复

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

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