Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Listener_Communication - Fatal编程技术网

Java线程间通信未传递消息对象

Java线程间通信未传递消息对象,java,multithreading,listener,communication,Java,Multithreading,Listener,Communication,为了在线程之间进行通信,我使用了Oracle,它可以很容易地编译和运行。我的体系结构稍有不同,因为我的消费者产生了生产者任务,尽管我尝试了这个示例的变体,但效果非常好 我的主程序中的相关代码 public static void main(String[] args) { ... FrameMsg frameMsg = new FrameMsg(); AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics(fra

为了在线程之间进行通信,我使用了Oracle,它可以很容易地编译和运行。我的体系结构稍有不同,因为我的消费者产生了生产者任务,尽管我尝试了这个示例的变体,但效果非常好

我的主程序中的相关代码

public static void main(String[] args) {
...
    FrameMsg frameMsg = new FrameMsg();
    AwarenessAnalytics awarenessAnalytic = new AwarenessAnalytics(frameMsg);
    awarenessAnalytic.start();
来自使用者线程的相关代码

public class AwarenessAnalytics extends Thread implements MotionEventListener{
    FrameMsg frameMsg;
    FrameWithMotionDetection frameWithMotionDetection;

      public AwarenessAnalytics(FrameMsg frameMsg) {
        this.frameMsg = frameMsg;
        System.out.println("AwarenessAnalytic frameMsg = " + this.frameMsg.hashCode());
        }
 AdvancedVideoAnalytics tempIntermediateVA;
 tempIntermediateVA = new AdvancedVideoAnalytics(frameMsg);

public void run() {

    tempIntermediateVA.start();

    while (true) {
        // TODO: create loop to process frames from each video stream
        frameWithMotionDetection = new FrameWithMotionDetection();
        // interthread message from AdvancedAnalytic
        System.out.println("Waiting for FrameMsg");
        frameWithMotionDetection = frameMsg.take();
        System.out.println("FrameMsg received");
}
生产者任务中的相关代码

public class AdvancedVideoAnalytics extends Thread {
  FrameMsg frameMsg;
  FrameWithMotionDetection frameWithMotionDetection;

public AdvancedVideoAnalytics (FrameMsg frameMsg) {
    this.frameMsg = frameMsg;
    System.out.println("AdvancedVideoAnalytic frameMsg = " + this.frameMsg.hashCode());
 }

// the run method includes;

// Send frame and any clusters detected
// as frameMsg
frameWithMotionDetection = new FrameWithMotionDetection();

frameWithMotionDetection.setMotionData(contourAnalysisResults);

frameWithMotionDetection.setCurrentFrame(frameToExamine);
System.out.println("Preparing to send message to AwarenessAnalytics thread");
frameMsg.put(frameWithMotionDetection);
FrameMsg类

public class FrameMsg {
// Message sent from video stream monitors to analytic fusion engine

private FrameWithMotionDetection frameWithMotionData;

//private String message;
// True if consumer should wait
// for producer to send message,
// false if producer should wait for
// consumer to retrieve message.
private boolean empty = true;

public synchronized FrameWithMotionDetection take() {
    // Wait until message is
    // available.
    System.out.println("Getting ready to take frameWithMotionData");
    while (empty) {
        try {
            wait(10);
            System.out.println("Waiting to take frameWithMotionData because empty = true");
        } catch (InterruptedException e) {}
    }
    // Toggle status.
    empty = true;
    System.out.println("Successfully took frameWithMotionData, empty = " + empty);
    // Notify producer that
    // status has changed.
    notifyAll();
    return frameWithMotionData;
}

public synchronized void put(FrameWithMotionDetection frameWithMotionData) {
    // Wait until message has
    // been retrieved.
    System.out.println("Getting ready to put frameWithMotionData");
    while (!empty) {
        try { 
            System.out.println("Waiting to put frameWithMotionData because empty = false");
            wait();
        } catch (InterruptedException e) {}
    }
    // Toggle status.
    empty = false;
    // Store message.
    this.frameWithMotionData = frameWithMotionData;
    System.out.println("Successfully put frameWithMotionData, empty = " + empty);
    // Notify consumer that status
    // has changed.
    notifyAll();
}
}

有趣的是,所有frameMsg对象ID都是相同的,我可以“放置”一个frameMsg,并从生成器中将其设置为空为false。但是,使用者看到的frameMsg对象总是为空返回“true”

输出摘录如下所示

VideoAnalyticsUnitTest frameMsg = 1704856573
AwarenessAnalytic frameMsg = 1704856573
AdvancedVideoAnalytic frameMsg = 1704856573

Waiting to take frameWithMotionData because empty = true
Waiting to take frameWithMotionData because empty = true
(many of these)...
Preparing to send message to AwarenessAnalytics thread
Getting ready to put frameWithMotionData
Successfully put frameWithMotionData, empty = false
Waiting to take frameWithMotionData because empty = true
Preparing to send message to AwarenessAnalytics thread
Getting ready to put frameWithMotionData
Waiting to put frameWithMotionData because empty = false
Waiting to take frameWithMotionData because empty = true
Waiting to take frameWithMotionData because empty = true
Waiting to take frameWithMotionData because empty = true
它就像最后三行一样继续,直到我终止程序

我很困惑,因为; 1.我仿效了这个例子 2.对象ID匹配

然而,使用者从未看到非空的frameMsg(这是一个复杂的对象)

我错过了什么明显的东西吗

我最初使用侦听器发送消息,但我不希望大型应用程序占用侦听器空间。现在阅读更多的评论,我似乎可以使用侦听器并通过阻塞队列将消息传递给消费者的运行部分


如果是您,您会采用上述通信方法,还是返回到具有阻塞队列的侦听器?

如果要更新
empty
参数,请不要使用
synchronized
方法
synchronized

synchronized(this){
//work from here for core logic 

}
//empty =true logic or empty = false
块确实比方法有优势,最重要的是灵活性,因为您可以使用其他对象作为锁,而同步方法将锁定整个类

比较:

// locks the whole object
... 
private synchronized void someInputRelatedWork() {
... 
}
private synchronized void someOutputRelatedWork() {
... 
}
Vs

此外,如果方法增长,您仍然可以保持同步部分分离:

 private void method() {
 ... code here
 ... code here
 ... code here
 synchronized( lock ) { 
    ...very few lines of code here
 }
 ... code here
 ... code here
 ... code here
 ... code here
}

最后一个建议是使用
LinkedBlockingQueue
,因为它有很好的性能命中率,并且有很好的方法
put-and-take

,正如@Bhargav Modi所指出的,该代码正在遇到编写多线程应用程序的更精细的问题(同步块与-方法,对关键变量使用
volatile
声明)。这些问题在测试过程中经常会被忽略,因为出现这些问题需要一个偶然因素(最臭名昭著的问题之一是)


这是使用类的一个很好的理由:编写非线程安全或有多线程问题的代码的可能性较小。在您的情况下,看起来是一个很好的替代品。对于SynchronousQueue,无需使用
empty
变量、
This.frameWithMotionData
变量或wait/notifyAll机制NIC.

您发布的代码没有编译。您能检查它并添加awarenessalytics和AdvancedVideoAnalytics的run()方法的内容吗?还有take()和put()方法在代码中的任何地方都不会调用方法。好的,只是添加了run方法的关键行。如果我包含所有代码,那么会有太多的代码。这离SSCeApologies很远,Giulio Franco,我会在保留相关部分的同时尽量删除。我已经删除了任何多余的代码,谢谢你的建议如果你认为这是一个合理的解释,Bhargav,非常感谢你,这对我的理解有帮助,我可以在将来使用它。在某种程度上,虽然前面的答案对我来说是有效的,而且更灵活、更不复杂。如果我可以将两者都设置为“选中”,我会这样做,但我只是尝试了一下,只允许一个。我采纳了你的建议,并通过实现ArrayBlockingQueue对其进行了轻微调整,现在它可以根据需要工作。谢谢你的建议!
 private void method() {
 ... code here
 ... code here
 ... code here
 synchronized( lock ) { 
    ...very few lines of code here
 }
 ... code here
 ... code here
 ... code here
 ... code here
}