- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了在线程之间进行通信,我遵循了Oracle Guarded Blocks example ,它很容易编译并运行。我的架构略有不同,因为我的消费者生成了生产者任务,尽管我在示例中尝试了这种变体并且它工作得很好。
我的主程序中的相关代码;
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(这是一个复杂的对象)。
我错过了一些明显的事情吗?
我最初使用监听器发送消息,但我不希望巨大的应用程序占用监听器空间。现在阅读更多评论,似乎我可以使用监听器并使用阻塞队列将消息传递给消费者的运行部分。
如果是您,您会采用上述通信方法,还是恢复为具有阻塞队列的监听器?
最佳答案
正如@Bhargav Modi 指出的那样,代码遇到了编写多线程应用程序的更精细问题(同步块(synchronized block)与方法,在关键变量上使用 volatile
声明)。这些问题在测试过程中经常被遗漏,因为出现这些问题需要一定的机会(最臭名昭著的问题之一是 double checked locking )。
这是使用 Java concurrent 的充分理由类:编写非线程安全或存在多线程问题的代码的可能性较小。在您的情况下,SynchronousQueue看起来是一个很好的替代品。使用 SynchronousQueue,无需使用 empty
变量、this.frameWithMotionData
变量或 wait/notifyAll 机制。
关于Java线程间通信不传递消息对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29089699/
大多数语言都支持双向进程通信。例如,在 Python 中,我可以(草率地)执行以下操作: >>> from subprocess import * >>> p = Popen('nslookup',
致力于使用 C++ 在 arduino 和 PC (Win 7) 之间进行通信。使用 WriteFile 和 ReadFile 创建通信或简单地发送或接收数据没有问题。但是当我想以某种方式“协调”沟通
我们正在开发一个基于微服务的应用程序。它们将使用 Helm Package Manager 部署到 kubernetes,并且它们都存储了自己的存储库和 helm chart。以下是我们微服务的名称。
我正在开发一个大型 MVVM 应用程序。我为此使用了 MVVM 轻量级工具包。该应用程序就像一个带有后退和前进按钮的网络浏览器。主视图是一个用户控件。我在主视图用户控件中放置了后退和前进按钮。主视图又
我在 java 和 freepascal(lazarus) 应用程序之间的通信有问题。我使用套接字。它们正确连接。一切都很顺利,直到我想从一个应用程序向另一个应用程序发送一些东西。在java而不是“a
我已经使用客户端套接字和服务器套接字使用C#编写了群聊。 当我使用VS 2017在自己的PC中运行程序(服务器和客户端)时,客户端和服务器之间的通信工作正常。 当我在笔记本电脑中运行客户端程序,并在自
Kubernetes 中两个不同 Pod 之间的通信是如何发生的? 就我而言,我有两个 Pod:前端和后端,它们都有不同的容器。 我希望我的前端 pod 与后端 pod 通信,但我不想使用后端 pod
我正在尝试在浏览器中嵌入的 flash 实例与在 C# WinForms 应用程序中运行的 flash 实例之间进行通信...我收到一个编译错误,内容为: 1119 Access of possibl
鉴于网络上缺乏信息,请问一个问题:我要在 Android 中创建一个应用程序,使用一个数据库应用程序 rails 。为此,我需要一个手动 session 。所以如果有人准备好了示例/教程显示通信 an
我正在编写一个应用程序,它将通过 MySQL 数据库对用户进行身份验证。我已经用 Java (android) 编写了它,但现在正在移植到 Windows 手机。 PHP 文件使用 $get 然后回显
是否可以通过互联网在两个不同设备上的两个不同应用程序之间建立通信。我想从设备 A 上的应用程序点击一个设备 B 上的应用程序,然后从设备 B 上的应用程序获取数据到设备 A 上的应用程序。如果可能,如
这是脚本: 它被放置在其他网站上。 com 并显示一个 iframe。如果有人点击 iframe 中的某个内容,脚本应该将一个 div 写入 othersite 。 com. 所以我的问题是如何做到
你好我是 php 的新手,我用 c++ 编写了整个代码并想在 php 中使用这段代码。所以我为我的代码制作了 dll 以使用它。但是我不能在 php 中使用这个 dll,可以谁能给我完整的代码来使用
我确定之前已经有人问过(并回答过)此类问题,所以如果是这样,请将我链接到之前的讨论... 在 C++ 中,假设我有一个 ClassA 类型的对象,其中包含一个 ClassB 类型的私有(private
我正在尝试使用 ATmega32 进行串行通信。首先,我使用 RS232,使用 USB-to-RS232 建立使用串行终端的接收和传输(在我的例子中是 tera 术语)。无论我从串行终端 Atmega
我找不到适用于 Ruby 的 SSL 实现。 我的部分项目需要服务器和客户端之间的安全通信链接,我希望为此使用 SSL 以创建安全 session 。 谢谢 最佳答案 如果你使用 Ruby 1.9.x
我正在尝试在客户端/服务器之间进行 SSL 通信。 到目前为止,我已经从 keystore 创建了 java.security.cert.X509Certificate。接下来我应该怎么做才能使这次沟
我在与 Windows 上的 USB 设备 通信时遇到问题。我不能使用 libusb 或 WinUSB,因为我有一个特定的驱动程序(Silabs USB 到 UART,这是一个 USB 到串口的桥接器
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我发现 xcom 实际上是将数据写入数据库并从其他任务中提取数据。我的数据集很大,将其腌制并写入数据库会导致一些不必要的延迟。有没有办法在不使用 xcom 的情况下在同一 Airflow Dag 中的
我是一名优秀的程序员,十分优秀!