.NET 消费者/生产者(队列)
我是 .NET/Threads 的新手,我想知道是否有人可以帮助我完成这项练习。我需要替换注释,以便使其在不锁定线程的情况下工作:
private Queue<int> queue;
public void ConsumeFunc(){
while (true){
// 1
while (/* 2 */){
// 3
}
int element = queue.Dequeue();
// 4
Console.WriteLine("Consume element: " + element);
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
private void ProduceFunc(){
while (true) {
// 1
queue.Enqueue(DateTime.Now.Millisecond);
// 2
// 3
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
我管理了此操作,但尚未完成
public void ConsumerFunc(){
while (true){
Monitor.PulseAll(queue); // 1
while (queue.Count == 0){ /* 2 */
Monitor.Wait(queue); // 3
}
int element = queue.Dequeue();
lock (queue) // 4
Console.WriteLine("Consume element: " + element);
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
public void ProducerFunc(){
while (true) {
lock (queue) // 1
queue.Enqueue(DateTime.Now.Millisecond);
Monitor.PulseAll(queue); // 2
// 3 ???
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 3) * 1000);
}
}
并给出以下错误: 从不同步的代码块调用对象同步方法, 上Monitor.PulseAll(queue);
I'm new at .NET/Threads and I'd like to know if anyone can help me on this exercise. I need to replace the comments in order to make it work without locking the threads:
private Queue<int> queue;
public void ConsumeFunc(){
while (true){
// 1
while (/* 2 */){
// 3
}
int element = queue.Dequeue();
// 4
Console.WriteLine("Consume element: " + element);
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
private void ProduceFunc(){
while (true) {
// 1
queue.Enqueue(DateTime.Now.Millisecond);
// 2
// 3
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
I managed this but it's not completed
public void ConsumerFunc(){
while (true){
Monitor.PulseAll(queue); // 1
while (queue.Count == 0){ /* 2 */
Monitor.Wait(queue); // 3
}
int element = queue.Dequeue();
lock (queue) // 4
Console.WriteLine("Consume element: " + element);
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 2) * 1000);
}
}
public void ProducerFunc(){
while (true) {
lock (queue) // 1
queue.Enqueue(DateTime.Now.Millisecond);
Monitor.PulseAll(queue); // 2
// 3 ???
Thread.Sleep(new Random((int)DateTime.Now.Ticks).Next(0, 3) * 1000);
}
}
and gives the following error:
Object synchronization method was called from an unsynchronized block of code,
on Monitor.PulseAll(queue);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其自己努力实现同步,不如看看 BlockingCollection。它会为您处理所有同步,并且比您使用 Queue 类和 Monitor 创建的任何内容都表现得更好。
.NET 文档、Stack Overflow 以及其他地方都有很多示例。您可能会发现我的文章很有用:http://www.informit。 com/guides/content.aspx?g=dotnet&seqNum=821
Rather than fighting to implement the synchronization yourself, take a look at BlockingCollection. It handles all the synchronization for you, and will perform better than anything you can create using the
Queue
class andMonitor
.There are plenty of examples in the .NET documentation, here on Stack Overflow, and elsewhere. You might find my article useful: http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=821