简体   繁体   English

并发执行Java 8

[英]Concurrent Execution Java 8

I am new to concurrent programming with java and trying to start some Callables asynchronously. 我是java并发编程的新手,试图异步启动一些Callables But the code seems to block my programm flow, where the Callables are given to the executorService es.invokeAll(tasks) : 但是代码似乎阻塞了我的程序流程,在此流程中, Callables被赋予executorService es.invokeAll(tasks)

public void checkSensorConnections(boolean fireEvent) {
    List<Callable<Void>> tasks = new ArrayList<>();

    getSensors().forEach(sensor -> {
        tasks.add(writerService.openWriteConnection(sensor));
        tasks.add(readerService.openReadConnection(sensor));
    });

    try {
        LOG.info("Submmitting tasks");

        ExecutorService es = Executors.newWorkStealingPool();
        es.invokeAll(tasks);

        LOG.info("Tasks submitted");
    } catch (InterruptedException e) {
        LOG.error("could not open sensor-connections", e);
        error(MeasurmentScrewMinerError.OPEN_CONNECTION_ERROR);
    }
}

I have some log statements controlling the flow of the program. 我有一些控制程序流程的日志语句。 As you can see is that the execution waits until the two tasks are executed. 如您所见,执行将等到两个任务执行完毕。

2017-01-19 16:06:06,474 INFO [main] de.cgh.screwminer.service.measurement.MeasurementService (MeasurementService.java:127) - Submmitting tasks 2017-01-19 16:06:06,474信息[主要] de.cgh.screwminer.service.measurement.MeasurementService(MeasurementService.java:127)-细分任务

2017-01-19 16:06:08,477 ERROR [pool-2-thread-2] de.cgh.screwminer.service.measurement.SensorReadService (SensorReadService.java:68) - sensor Drehmoment read-connection could not be opened java.net.SocketTimeoutException: Receive timed out ... 2017-01-19 16:06:08,477错误[pool-2-thread-2] de.cgh.screwminer.service.measurement.SensorReadService(SensorReadService.java:68)-传感器Drehmoment读取连接无法打开java。 net.SocketTimeoutException:接收超时...

2017-01-19 16:06:08,477 ERROR [pool-2-thread-4] de.cgh.screwminer.service.measurement.SensorReadService (SensorReadService.java:68) - sensor Kraft read-connection could not be opened java.net.SocketTimeoutException: Receive timed out ... 2017-01-19 16:06:08,477错误[pool-2-thread-4] de.cgh.screwminer.service.measurement.SensorReadService(SensorReadService.java:68)-传感器Kraft读取连接无法打开java。 net.SocketTimeoutException:接收超时...

2017-01-19 16:06:08,482 INFO [main] de.cgh.screwminer.service.measurement.MeasurementService (MeasurementService.java:132) - Tasks submitted 2017-01-19 16:06:08,482信息[主要] de.cgh.screwminer.service.measurement.MeasurementService(MeasurementService.java:132)-提交的任务

From the Javadoc of invokeAll: 从invokeAll的Javadoc中:

Returns:
   a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed

So yes invokeAll Tasks are finished. 因此,invokeAll任务已完成。

What you can do is just hold the Executor in the class and submit each task in your forEach() which would do the same. 您可以做的就是将Executor放在类中,然后在forEach()中提交每个任务,这样做也可以。 You then get a list of Futures which you should check for errors. 然后,您将获得一份期货清单,应检查是否存在错误。

you could do something like this: 您可以执行以下操作:

getSensors().forEach(s -> {
    CompletableFuture<Void> cf = (s -> writerService.openWriteConnection(s)).exceptionally(ex -> errorhandling)
    exec.submit(cf)
});

CompletableFuture is a Java8 feature and lets you control errors nicely as you don't have to ask the Futures if they completed successfully (Which often leads to unexpected non logged errors) CompletableFuture是Java8的一项功能,可让您很好地控制错误,因为您不必询问Future是否成功完成(通常会导致意外的未记录错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM