在 C# 中锁定并行线程的单个访问变量
你好,我有这段代码
var queue = new BlockingCollection<int>();
queue.Add(0);
var producers = Enumerable.Range(1, 3)
.Select(_ => Task.Factory.StartNew(()=>
{
Enumerable.Range(1, queue.Count)
.ToList().ForEach(i =>
{
lock (queue)
{
if (!queue.Contains(i))
{
Console.WriteLine("Thread" + Task.CurrentId.ToString());
queue.Add(i);
}
}
Thread.Sleep(100);
});
}))
.ToArray();
Task.WaitAll(producers);
queue.CompleteAdding();
foreach (var item in queue.GetConsumingEnumerable())
{
Console.WriteLine(item.ToString());
}
,但每次单个线程向队列中添加一些内容时,我都需要。Add(i) Enumerable.Range(1,queue.Count) 被增加,以便代码执行,直到没有更多的项目添加到队列中。我希望你能理解这个问题。 换句话说,我需要这个动作无限运行,直到我告诉它停止。 有什么建议吗?
Hello i have this code
var queue = new BlockingCollection<int>();
queue.Add(0);
var producers = Enumerable.Range(1, 3)
.Select(_ => Task.Factory.StartNew(()=>
{
Enumerable.Range(1, queue.Count)
.ToList().ForEach(i =>
{
lock (queue)
{
if (!queue.Contains(i))
{
Console.WriteLine("Thread" + Task.CurrentId.ToString());
queue.Add(i);
}
}
Thread.Sleep(100);
});
}))
.ToArray();
Task.WaitAll(producers);
queue.CompleteAdding();
foreach (var item in queue.GetConsumingEnumerable())
{
Console.WriteLine(item.ToString());
}
But i need each time that a single thread ads something to the queue.Add(i) the
Enumerable.Range(1, queue.Count) to be inceased so that the code executes until there are no more items to be added to the queue. I hope you understand the question.
In other words i need this action to run infinitely untill i tell it to stop.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很抱歉地说,但如果没有进一步的解释,我无法理解你写这样的东西的动机:(
以下代码对你有任何用处吗?因为我不认为它是:P
我的意思是,它会一直这样下去。这就像一种奇怪的方式,只是将数字添加到列表中。
请解释一下您的目标,我们也许可以帮助您。
I´m sorry to say, but I can´t understand your motives for writing something like that without further explaination :(
Is the following code useful to you in any way? Because I don´t think it is :P
I mean, it will just go on and on. It´s like a reeeeeeaaallllyyy strange way of just adding numbers to a List
Please explain you objective and we might be able to help you.
我明白了,您需要的是
BlockingCollection
随 .NET 4.0 一起提供。它允许实现生产者-消费者模式。
多个线程或任务可以同时将项目添加到集合中。多个消费者可以同时删除项目,如果集合变空,消费线程将阻塞并等待,直到生产者添加项目。一遍又一遍……
直到生产者调用一个特殊的方法来识别结束,告诉消费者“嘿,别再等了——什么也不会来了!”。
我不会发布代码示例,因为给定的链接下有一些代码示例。如果您只需在 google 上搜索生产者-消费者模式和/或
BlockingCollection
,您就可以找到更多信息。I see, what you need is a
BlockingCollection
which came with .NET 4.0.It allows to implement the Producer-Consumer pattern.
Multiple threads or tasks can add items to the collection concurrently. Multiple consumers can remove items concurrently, and if the collection becomes empty, the consuming threads will block and wait until a producer adds an item. Over and over again ...
... until a special method will be called by producer to identify the end, saying consumer "Hey, stop waiting there - nothing will come anymore!".
I am not posting code samples, because there are some under given link. You can find much more if you just google for Producer-Consumer pattern and/or
BlockingCollection
.