Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA多线程环境中的同步问题及流接口_Java_Multithreading_Java Stream - Fatal编程技术网

JAVA多线程环境中的同步问题及流接口

JAVA多线程环境中的同步问题及流接口,java,multithreading,java-stream,Java,Multithreading,Java Stream,我遇到了一个相当复杂的多线程问题: 我运行一个将数据推送到activeMQ消息队列的服务,该服务完全独立于我正在运行的实际应用程序 推送到ActiveMQ的数据通过apachecamel处理器进行拾取和处理,监听器可以在该处理器中注册以获得关于新处理器结果的通知 实际应用程序是Camel处理器的侦听器。它通过stream()接口驱动进程,在每个处理步骤启动新线程 目标是使单独线程中的代码等待ActiveMq处理器的结果。我相信我必须使用一些线程锁定或闩锁,但我在JAVA多线程方面没有足够的经

我遇到了一个相当复杂的多线程问题:

  • 我运行一个将数据推送到activeMQ消息队列的服务,该服务完全独立于我正在运行的实际应用程序
  • 推送到ActiveMQ的数据通过apachecamel处理器进行拾取和处理,监听器可以在该处理器中注册以获得关于新处理器结果的通知
  • 实际应用程序是Camel处理器的侦听器。它通过stream()接口驱动进程,在每个处理步骤启动新线程
目标是使单独线程中的代码等待ActiveMq处理器的结果。我相信我必须使用一些线程锁定或闩锁,但我在JAVA多线程方面没有足够的经验来实现这一点

处理类代码:

public class MyApplication {

    CallingInstanceIF _callingInstance = null;
    ActiveMQConnector _activemqConector = null;
    Data _dataFromApacheCamelProcessor = null;

    public MyApplication(CallingInstanceIF callingInstance) throws Exception {

        _callingInstance = callingInstance;

        // start apache camel route to ActiveMQ server. 
        //This pushes data at arrival to method doOnDataArrival() below
        try {
            _activemqConnector = new ActiveMQConnector(this);
            _activemqConnector.startConnections();
        } catch (Exception e) {
            ...
        }
    }

    private ResultDataStructure methodExecutedInSeparateThread(inputData) {

            _doSomethingLatch = new CountDownLatch(1);
            _callingInstance.setCountDownLatch(_doSomethingLatch);
            ResultDataStructure output = new ResultDataStructure();

            try {
               // do something in calling instance. 
               // This is done synchronously in other thread
                resultOfDoSomething = _callingInstance.doSomething(inputData);
            } catch (final Exception ex) {
                ...
            }

            // wait for _callingInstance to 
            // finish --> _callingInstance lifts _doSomethingLatch (working code up to here)
            try {
                _doSomethingLatch.await();
            } catch (final InterruptedException ex) {
                ...
            }

            output.add(resultOfDoSomething);

            // Here is where I have problems: 
            //this method shall for result arriving from method doOnDataArrival() below

            waitForCamelResult();
            output.add(_dataFromApacheCamelProcessor);


        return output

    }

    public void initAlgorithm(final Object scanParameters) throws Exception {

                _scannerEngine = Engine.builder(this::methodExecutedInOtherThread, inputData) //
                        .executor(Executors.newSingleThreadExecutor()) // Executor kicks off "methodExecutedInSeparateThread()" above
                        .build();

        }
    }

    public void mainProcessingMethod(final BiConsumer<...> callback) throws Exception {

        final Thread processingThread = new Thread(() -> {

            try {
                _scannerEngine.stream() //
                        .limit(... limit criteria) //
                        .forEach(processResult -> {
                            waiting();
                            if (callback != null) {
                                    callback.accept(evolutionResult, best.max());
                                }
                            }
                        });

            } catch (final Exception ex) {
                ...
            }

            _callingInstance.doOnAlgorithmFinished();
        });

        processingThread.start();
        _engineStream = processingThread;
    }    

    public void doOnDataArrival(dataFromApacheCamelProcessor) {

        _dataBuffer = dataFromApacheCamelProcessor;

        shall notify "methodExecutedIn
    }
}
公共类MyApplication{
callingInstance如果_callingInstance=null;
ActiveMQConnector\u activemqConector=null;
数据_dataFromApacheCamelProcessor=null;
公共MyApplication(callingInstance如果callingInstance)引发异常{
_callingInstance=callingInstance;
//启动到ActiveMQ服务器的apache骆驼路由。
//这会将到达时的数据推送到下面的方法doondaarrative()
试一试{
_activemqConnector=新的activemqConnector(此);
_activemqConnector.startConnections();
}捕获(例外e){
...
}
}
私有结果数据结构方法ExecutedInSeparateThread(inputData){
_doSomethingLatch=新的倒计时锁存器(1);
_调用instance.setCountDownLatch(_doSomethingLatch);
ResultDataStructure输出=新的ResultDataStructure();
试一试{
//在调用实例中执行一些操作。
//这是在其他线程中同步完成的
ResultToDoSomething=\调用Instance.doSomething(输入数据);
}捕获(最终异常){
...
}
//等待(u callingInstance)呼叫
//完成-->\u调用Instance lifts\u doSomethingLatch(工作代码到此为止)
试一试{
_doSomethingLatch.wait();
}捕获(最终中断例外){
...
}
output.add(resultOfDoSomething);
//以下是我遇到的问题:
//该方法应适用于以下方法doondaarrative()得出的结果
waitForCamelResult();
add(_dataFromApacheCamelProcessor);
返回输出
}
公共void initAlgorithm(最终对象扫描参数)引发异常{
_scannerEngine=Engine.builder(此::方法在其他线程中执行,inputData)//
.executor(Executors.newSingleThreadExecutor())//executor启动上面的“methodExecutedInSeparateThread()”
.build();
}
}
public void mainProcessingMethod(最终双消费者回调)引发异常{
最终线程处理线程=新线程(()->{
试一试{
_scannerEngine.stream()//
.限制(…限制标准)//
.forEach(processResult->{
等待();
if(回调!=null){
accept(evolutionResult,best.max());
}
}
});
}捕获(最终异常){
...
}
_调用instance.doOnAlgorithmFinished();
});
processingThread.start();
_engineStream=processingThread;
}    
公共无效DoondaarRival(来自ApacheCamelProcessor的数据){
_dataBuffer=来自ApacheCamelProcessor的数据;
应通知“已执行的方法”
}
}

我无法参考上面的代码,但据我所知,有两种java结构可能对您有所帮助

()可以在需要发送线程并等待单个响应时帮助您。使用
supplyAsync
get


或者,如果您需要在一个实例中传递多个“快照”,您可以使用()(阻塞队列)。基本上调用
put
take

Hi Ben,我使用在methodExecutedInSeparateThread()中实例化并在DoondatarRival()中倒计时的静态CondungLatch实现了所需的功能.这是可行的,也许它不是很优雅…在期货方面也发挥了很好的作用,但这更难使用。