首页 \ 问答 \ 如何在使用LinkedBlockingQueue时优先考虑消费者?(How do I give priority to Consumers when using a LinkedBlockingQueue?)

如何在使用LinkedBlockingQueue时优先考虑消费者?(How do I give priority to Consumers when using a LinkedBlockingQueue?)

我正在使用LinkedBlockingQueue和生产者/消费者模式一起来缓冲任务。 将任务添加到队列中我使用了我的生产者的方法:Queue.put(Object); 为我的消费者使用队列中的任务:Queue.take(Object);

我在Java API中发现,这两种方法都会阻塞,直到队列变为可用。 我的问题是:我知道有一个事实,即我的系统中有更多的生产者,然后是消费者。 我所有的任务都需要处理。 所以我需要我的消费者在被阻止时优先于生产者获得队列。

他们是否在不更改LinkedBlockingQueue方法的情况下做到这一点?


I am using a LinkedBlockingQueue together with the producer/consumer pattern to buffer tasks. To add tasks to the queue I use the method for my producers: Queue.put(Object); To take a task form my queue I use for my consumers: Queue.take(Object);

I found in the Java api that both these methods will block until they the queue becomes available. My problem is: I know for a fact that there are more producers of tasks in my system then consumers. And all my tasks need to be processed. So I need my consumers, when blocked, to have priority over the producers to get the queue.

Is their a way to do this without changing the methods of LinkedBlockingQueue to much?


原文:https://stackoverflow.com/questions/17748259
更新时间:2024-02-26 11:02

最满意答案

它几乎听起来像树这样的分层结构更适合显示这些数据。 但是,要回答您的问题,您希望“展平”数据的嵌套列表性质(因此,您拥有嵌套的foreach循环),而无需每次手动创建新列表。

在不了解您所拥有的任何基础设施的情况下,这里可能会有所作为,尽管有更优雅的方法可以做到这一点。

首先,添加另一个属性,该属性将表示要在ListView中显示的展平数据(有点像你在UpdateBindBaseNodList中所做的那样)。

public IEnumerable<String> BaseNodeListFlattened
{
  get
  {
    foreach (var baseNode in BaseNodes)
    {
      foreach (ushort matId in baseNode.MatIDBitArrayDictionary.Keys)
      {
        yield return String.Format("{0} - {1}", baseNode.Name, matId);
      }
    }
  }
}

这可以随时为您提供列表。 现在您只需要一种方法来告诉View它已经更新。

ObservableCollection很好地满足了我们的需求。
给定ObservableCollection<BaseNode> BaseNodes; ...
我们可以执行此操作(可能在ViewModel的构造函数中),以便在您更改列表时随时告诉View。

this.BaseNodes.CollectionChanged += (s, e) => this.OnPropertyChanged("BaseNodeListFlattened");

现在,每次向BaseNodes添加节点时,它都会触发CollectionChanged事件,然后调用OnPropertyChanged处理程序并通知View更新列表,它将读取BaseNodeListFlattened并刷新其项列表。

当然还有ListView的相应xaml ......

<ListView ItemsSource="{Binding BaseNodeListFlattened}"/>

It almost sounds like a hierarchical structure like a tree would be better suited to display this data. However, to answer your question, you want to "flatten" out the nested list nature of your data (hence the nested foreach loops you have) without manually creating a new list each time.

Without knowing any of the infrastructure you have in place, here is something that might work, albeit there are more elegant ways to do this.

First, add another property that will represent the flattened data you want to display in your ListView (kind of like you are doing in UpdateBindBaseNodList).

public IEnumerable<String> BaseNodeListFlattened
{
  get
  {
    foreach (var baseNode in BaseNodes)
    {
      foreach (ushort matId in baseNode.MatIDBitArrayDictionary.Keys)
      {
        yield return String.Format("{0} - {1}", baseNode.Name, matId);
      }
    }
  }
}

This gets you the list anytime you ask for it. Now you just need a way to tell the View that it has been updated.

An ObservableCollection does nicely for our needs.
Given ObservableCollection<BaseNode> BaseNodes; ...
We can do this (perhaps in the constructor of your ViewModel) to tell the View anytime you change the list.

this.BaseNodes.CollectionChanged += (s, e) => this.OnPropertyChanged("BaseNodeListFlattened");

Now every time you add a node to your BaseNodes it will trigger the CollectionChanged event which will then call the OnPropertyChanged handler and inform the View to update the list, where it will read BaseNodeListFlattened and refresh its list of items.

And of course the corresponding xaml for the ListView...

<ListView ItemsSource="{Binding BaseNodeListFlattened}"/>

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。