gpt4 book ai didi

C# 生产者/消费者模式

转载 作者:太空狗 更新时间:2023-10-29 20:06:31 33 4
gpt4 key购买 nike

我有如下简单的单生产者/双消费者代码,但输出显示只有 C2 正在消耗。我的代码中有错误吗?

class Program
{
static void Main(string[] args)
{
Object lockObj = new object();
Queue<string> queue = new Queue<string>();
Producer p = new Producer(queue, lockObj);
Consumer c1 = new Consumer(queue, lockObj, "c1");
Consumer c2 = new Consumer(queue, lockObj, "c2");

Thread t1 = new Thread(c1.consume);
Thread t2 = new Thread(c2.consume);
t1.Start();
t2.Start();

Thread t = new Thread(p.produce);
t.Start();

Console.ReadLine();
}
}
public class Producer
{
Queue<string> queue;
Object lockObject;
static int seq = 0;
public Producer(Queue<string> queue, Object lockObject)
{
this.queue = queue;
this.lockObject = lockObject;
}

public void produce()
{
while( seq++ <15) //just testinng 15 items
{
lock (lockObject)
{
string item = "item" + seq;
queue.Enqueue(item);
Console.WriteLine("Producing {0}", item);
if (queue.Count == 1)
{ // first
Monitor.PulseAll(lockObject);
}
}
}
}

}

public class Consumer
{
Queue<string> queue;
Object lockObject;
string name;
public Consumer(Queue<string> queue, Object lockObject, string name)
{
this.queue = queue;
this.lockObject = lockObject;
this.name = name;
}

public void consume()
{
string item;
while (true)
{
lock (lockObject)
{
if (queue.Count == 0)
{
Monitor.Wait(lockObject);
continue;
}
item = queue.Dequeue();
Console.WriteLine(" {0} Consuming {1}", name, item);
}
}
}
}

输出是:

Producing item1
c2 Consuming item1

Producing item2
c2 Consuming item2

Producing item3
c2 Consuming item3

Producing item4
c2 Consuming item4

Producing item5
c2 Consuming item5

Producing item6
c2 Consuming item6

Producing item7
c2 Consuming item7

Producing item8
c2 Consuming item8

Producing item9
c2 Consuming item9

Producing item10
c2 Consuming item10

Producing item11
c2 Consuming item11

Producing item12
c2 Consuming item12

Producing item13
c2 Consuming item13

Producing item14
c2 Consuming item14

Producing item15
c2 Consuming item15

最佳答案

首先,我无法重现您的问题,这里两个线程都消耗了一些项目。我猜你的机器速度更快,但像 gw 建议的那样添加 Sleep 会解决这个问题。我还建议您不要尝试同步生产者,我的意思是让它尽可能快地对项目进行排队,并让消费者同步以查看谁处理每个项目。我做了一个快速修改,它似乎工作正常:

static void Main()
{
Object lockObj = new object();
Queue<string> queue = new Queue<string>();
Producer p = new Producer(queue);
Comsumer c1 = new Comsumer(queue, lockObj, "c1");
Comsumer c2 = new Comsumer(queue, lockObj, "c2");

Thread t1 = new Thread(c1.consume);
Thread t2 = new Thread(c2.consume);
t1.Start();
t2.Start();

Thread t = new Thread(p.produce);
t.Start();

Console.ReadLine();
}
}
public class Producer
{
Queue<string> queue;
static int seq;
public Producer(Queue<string> queue)
{
this.queue = queue;
}

public void produce()
{
while (seq++ < 1000) //just testinng 15 items
{
string item = "item" + seq;
queue.Enqueue(item);
Console.WriteLine("Producing {0}", item);
}
}
}

public class Comsumer
{
Queue<string> queue;
Object lockObject;
string name;
public Comsumer(Queue<string> queue, Object lockObject, string name)
{
this.queue = queue;
this.lockObject = lockObject;
this.name = name;
}

public void consume()
{
string item;
while (true)
{
lock (lockObject)
{
if (queue.Count == 0)
{
continue;
}
item = queue.Dequeue();
Console.WriteLine(" {0} Comsuming {1}", name, item);
}
}
}
}

您还可以添加 sleep 以减慢消费者循环。

关于C# 生产者/消费者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1371249/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com