使用UI切换的Java生产者消费者 [英] Producer Consumer in Java with a UI Toggle

查看:138
本文介绍了使用UI切换的Java生产者消费者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试并且失败,在java中实现Producer Consumer模式,受以下约束条件限制:

I am trying, and failing, to implement a Producer Consumer pattern in java, subject to the following constraints:


  • Producer生成进入(和消费者消费)有限大小的队列

  • 有一个用户界面,按钮分别切换生产者和消费者

  • 生产者只应在队列未满生成器按钮切换为活动时生成,

  • 消费者应该仅在队列不为空时消费并且消费者按钮切换为有效。

  • 生产和消费都应该同时进行。 (碰巧生产的消费量至少至少,有时更快。)

  • The Producer produces into (and the Consumer consumes from) a queue with finite size
  • There is a user interface with buttons toggling the Producer and Consumer, separately
  • The Producer should produce only when queue is not full and the producer button is toggled active,
  • The Consumer should consume only when the queue is not empty and the consumer button is toggled active.
  • Both production and consumption should be possible at the same time. (It so happens that production will be at least as possible as consumption, and sometimes faster.)

我的想法是将缓冲区实现为有限大小的LinkedBlockingQueue,以处理与队列空/满状态相关的条件 - 它应该在尝试放入完整队列或从空队列中取出时阻塞。然后,在生产者和消费者上使用布尔状态,触发按钮。最后在生产者和消费者中使用while / wait / loop,并在代码中使用notify来获取按钮。

My thought was to implement the buffer as a LinkedBlockingQueue of finite size, to handle the conditions relating to the queue empty/full states-- it should block when trying to put to a full queue or take from an empty one. Then, use a boolean state on the producer and the consumer, triggered off the buttons. Finally use a while/wait/loop in the producer and consumer, and a notify in the code for the buttons.

对于生产者方而言如下:

Something like the following, for the producer side:

  while (true) { 
    if (!producing) { wait(); }
    //  generate a bunch of data and and finally        
    //  Save this chunk of data
    buffer.addData(data);
  }

在生产者按钮的代码中,我们都切换生产状态和在自我通知的生产者上调用一个方法。

And in the code for the producer button, we both toggle the producing state and call a method on the producer that self notifies.

问题:一旦制作人正在制作,它就会如此努力地进行民意调查甚至UI(在Swing中实现)都会失去响应能力。我可以通过填写 wait(1); 语句来解决这个问题,但出于各种不可协商的原因,这是不可接受的。一些延迟是不可避免的,但每次通过循环1 ms延迟都不起作用。

Problem: Once the producer is producing, it polls so hard even the UI (implemented in Swing) loses responsiveness. I can fix that by wadding a wait(1); statement, but for various non-negotiable reasons that's just not acceptable. Some delay is inevitable, but a 1 ms delay each time through the loop just won't work.

我也不相信我对LinkedBlockingQueue有正确的理解,因为如果我让队列填满,我仍然会失去UI响应能力。我显然误解了Java中的串行操作是如何工作的,因为我尝试了多种方法,这是我得到的最接近的方法;试图在没有LinkedBlockingQueue的情况下手动处理完整/空条件的先前方法是悲惨的失败。

I'm also not convinced I have the right understanding of the LinkedBlockingQueue, either, since if I let the the queue fill up, I still lose UI responsiveness. I am clearly misunderstanding how theaded operations in Java work, as I've tried multiple approaches and this is the closest I've got; previous approaches trying to handle the full/empty conditions "manually" without the LinkedBlockingQueue were miserable failures.

任何建议都将受到赞赏。当然我正在尝试做的事情(在没有过多轮询的情况下锁定两个条件)并非不可能,是吗?

Any advice would be appreciated. Surely what I'm trying to do (lock on two conditions without excessive polling) isn't impossible, is it?

推荐答案

讨论包含该计划 TwoTimer ,其中 javax.swing.Timer 增加 int at 100 Hz,而 java.util.TimerTask 以1 Hz的频率对值进行采样。变体将样本存储在 LinkedBlockingQueue 中,形成最近的历史队列。它与您的任务没有直接关系,但它说明了响应式GUI的基本要求:永远不会阻止事件派发线程。该示例使用 invokeLater()来执行下一个集合。

This discussion includes the program TwoTimer, in which a javax.swing.Timer increments an int at 100 Hz, while a java.util.TimerTask samples the value at 1 Hz. A variation stores the samples in a LinkedBlockingQueue, forming a recent history queue. It's not directly related to your task, but it illustrates an essential requirement for a responsive GUI: never block the event dispatch thread. The example uses invokeLater() to perform the next collection.

还要考虑 nextGaussian() 模拟延迟,如此处所示。

顺便说一句,您可能会喜欢这个动画几何形状队列的示例

As an aside, you might enjoy this example that animates a queue of geometric shapes.

这篇关于使用UI切换的Java生产者消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆