如何在使用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
最满意答案
它几乎听起来像树这样的分层结构更适合显示这些数据。 但是,要回答您的问题,您希望“展平”数据的嵌套列表性质(因此,您拥有嵌套的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.
GivenObservableCollection<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 theCollectionChanged
event which will then call theOnPropertyChanged
handler and inform the View to update the list, where it will readBaseNodeListFlattened
and refresh its list of items.And of course the corresponding xaml for the ListView...
<ListView ItemsSource="{Binding BaseNodeListFlattened}"/>
相关问答
更多-
使用Eneumerable.SelectMany List
list = _ICarsDic.SelectMany(r => r.Value.brandsDetails) .ToList(); Use Eneumerable.SelectMany List list = _ICarsDic.SelectMany(r => r.Value.brandsDetails ... -
从包含列表并按值过滤的字典中获取键(对象)(Get keys (objects) from a dictionary that contains list and filtered by values)[2021-07-18]
我想你想要这样的东西: var myKeys = objAwithB.Where( kvp => kvp.Value.Any(v => v.prop1.Equals(objB.prop1) ) ).Select(kvp => kvp.Key); I think you want something like this: var myKeys = objAwithB.Where( kvp => kvp.Value.Any(v => v.prop1.Equals(objB.prop1) ) ... -
我想你已经写了你想要做的最有效的版本。 由于Linq不能很好地处理输出参数,因此需要稍长的时间。 但是你会这样做: var dict = Foo.FirstOrDefault(d => d.ContainsKey(key)); if (dict != null) { dict.TryGetValue(key, out name); } I think you have written the most efficient version of what you want to do. Since Linq ...
-
字典和列表的行为(The behavior of dictionary and list)[2023-01-15]
关键问题是你已经定义了b字典。 你已经在循环之前完成了它,但需要在内部完成: for p in post: b = {} b[p.pub_date.year] = p.pub_date.month a.append(b) 或者,使用列表理解 : a = [{p.pub_date.year: p.pub_date.month} for p in post] The key problem is where you've defined the b dictionary. You' ... -
如何从功能文件传递字典或列表或自定义对象(How to pass dictionary or list or custom objects from feature file)[2022-01-27]
据我所知,你不能传递特性中的对象,但你可以在特性文件中发送文本和表格。 您发送的文本可被利用来发送字典/列表的JSON字符串,并在步骤定义中使用该数据进一步创建对象。 As far as I know, you can't pass objects in features, but you can send text and tables in the feature file. The text you send can be exploited to send JSON string of your d ... -
它几乎听起来像树这样的分层结构更适合显示这些数据。 但是,要回答您的问题,您希望“展平”数据的嵌套列表性质(因此,您拥有嵌套的foreach循环),而无需每次手动创建新列表。 在不了解您所拥有的任何基础设施的情况下,这里可能会有所作为,尽管有更优雅的方法可以做到这一点。 首先,添加另一个属性,该属性将表示要在ListView中显示的展平数据(有点像你在UpdateBindBaseNodList中所做的那样)。 public IEnumerable
BaseNodeListFlattened ... -
您需要先分组,然后转换为字典。 像那样: var dictionary = tblValues .GroupBy(table => table.CodeID) .ToDictionary(values => values.Key); 如果你需要转换你的密钥,请在GroupBy()或ToDictionary() I found a way to do it, the_joric's answer was close but I had to explicitly call ToLis ...
-
这不是GridView,但是分组的ListView提供了非常好的结果: using System; using System.Collections.Generic; using System.Windows.Forms; using System.Linq; namespace so { public class Program { public static void Main() { Application.EnableVisu ...
-
这是一个很好的伎俩; 基本上,Dictionary可以被认为是KeyValuePair <>对象的List。 Dictionary
myDictionary = new Dictionary (); foreach(var item in myDictionary) // item is a KeyValuePair , enumerated in order of their insertion into the dictionar ... -
关于什么: list(d.items()) 它将创建一个元组列表,如: [('Foo',3),('Bar',2)] 或者您可以定义自己的类: class WordCount: def __init__(self,word,count): self.word = word self.count = count 并使用列表理解: [WordCount(*item) for item in d.items()] 所以在这里创建一个WordCount对象列表。 然而 ...