与普通线程实时线程同步

发布于 2024-07-13 19:51:16 字数 110 浏览 6 评论 0 原文

如何将一个实时线程与 Java 中的普通线程同步? 以计算一些温度的实时线程和必须收集这些数字并在某些控件上显示它们的 GUI 线程为例。

实时库的实现并不重要,因为它应该遵循规范。

How would one synchronize one real-time thread with a normal thread in Java? Take for example a real-time thread that calculates some temperatures and the GUI thread that has to collect those numbers and show them on some controls.

The implementation of the real-time library shouldn't matter as it should follow the specifications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-07-20 19:51:16

你需要两件事。 您希望实时线程获得优先级,并且最好由计时器驱动它,以便您获得(几乎)定期测量。 然后,您将需要可用于传达价值观的资源。 它可以是一个简单的监视器,带有一个关键部分,以便计时器线程可以写入其中,也可以是——而且很可能会更好——一个 FIFO,这样 GUI 线程就可以在任何时候吃掉值。有周期。

在 Java 中,GUI(至少在 Swing 和类似的中)已经运行一个单独的线程用于 UI 交互,因此您的大问题是设置测量线程。 看看 Runnables。

You're going to need two things. You want your real-time thread to get priority, and preferably have it driven by a timer so you get (nearly) periodic measurements. Then, you're going to want a resource that can be used to communicate the values. That can either be a simple monitor, with a critical section so the timer thread can write into it, or it could be -- and very probably would be better as -- a FIFO, so that the GUI thread can eat up values whenever it has cycles.

In Java, the GUI (at least in Swing and similar) is already running a separate thread for UI interactions, so you're big issue is to set up your measurement thread. Have a look at Runnables.

白昼 2024-07-20 19:51:16

要使用实时线程,您需要实时操作系统上的 Real Time Java。 http://java.sun.com/javase/technologies/realtime/index.html jsp

但是,如果您有一个对延迟敏感的线程,我建议您;

  • 使用并发库与其他线程通信。
  • 尽量减少任何 GC 活动(尤其是完整 GC)
  • 如果可以的话,不要在与 GUI 相同的进程中运行线程(因为它往往会以您无法控制的方式获取大量资源)

To use real-time threads you need Real Time Java on real time operating system. http://java.sun.com/javase/technologies/realtime/index.jsp

However if you have a thread which is latency sensitive, I suggest you;

  • use the concurrency libraries in communications with other threads.
  • minimise any GC activity (esp full GCs)
  • don't run the thread in the same process as a GUI if you can (as it tends grab a lot of resources in ways you have limited control over)
悲念泪 2024-07-20 19:51:16

既然其他人提出了RTSJ,我就评论一下实时和非实时代码之间的同步有很多解决方案。 RTSJ 为此类通信提供免等待队列。 还可以基于这些或其他队列进行构建,并利用 RTSJ 的 AsyncEvent 和 AsyncEventHandler 抽象来管理通信。 这适用于您确实需要“实时”线程的确定性行为的情况。

如果您可以接受尽力而为的行为(非常努力地按时完成任务,但即使您错过了,世界也不会崩溃),我建议您在 Java 并发实用程序。 仔细选择任务边界、合适的排队策略(这里,“合适”将取决于您的应用程序比您给出的更多细节)和线程池策略将满足您的需求。

Since others have brought up RTSJ, I'll comment that synchronization between real-time and non-real-time code has a number of solutions. RTSJ provides wait-free queues for such communication. It is also possible to build upon these or other queues and make use of RTSJ's AsyncEvent and AsyncEventHandler abstractions to manage the communication. This is appropriate for situations where you really, truly have a need for deterministic behavior for the "real-time" thread.

If you can accept best-effort behavior (try really hard to hit your deadlines, but the world doesn't fall apart if you miss) I suggest building carefully on the executor framework provided by the Java concurrency utilities. A careful selection of task boundaries, a suitable queuing policy (here, "suitable" would depend on more detail about your application than you've given), and threadpool policy will get what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文