- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是线程的新手,我正在尝试学习不同的概念。
现在我正在使用计时器线程执行生产者/消费者模式。问题是我不知道如何检查所有生产者和消费者线程是否在让 Timer 线程滴答一定时间之前完成了它们的进程,并为下一个滴答释放所有创建的生产者和消费者线程。
想寻求您的帮助和指导,了解如何为这种方法创建变通办法。
这是我的示例代码:
public class WorkerThread
{
public BlockingQueue<Item> collection = new BlockingQueue<Item>(100);
private Timer TimerThread { get; set; }
public void ThreadTimer()
{
this.TimerThread = new Timer(new TimerCallback(StartMonitor), null, 500, Timeout.Infinite);
}
public void StartMonitor(object state)
{
List<Thread> producers = new List<Thread>();
List<Thread> consumers = new List<Thread>();
for (int i = 0; i < 1; i++)
{
producers.Add(new Thread(() => RunProducers(this.collection)));
}
//TODO: Start all producer threads...
for (int i = 0; i < 2; i++)
{
consumers.Add(new Thread(() => RunConsumers(this.collection)));
}
//TODO: Start all consumer threads...
//TODO: Let Thread wait until all worker threads are done
//TODO: Dispose Threads
TimerThread.Change(5000, Timeout.Infinite);
}
public void RunProducers(BlockingQueue<Item> collection)
{
List<Item> lsItems = CreateListOfItems();
foreach(var item in lsItems)
{
collection.Add(item);
}
}
public void RunConsumers(BlockingQueue<Item> collection)
{
while(true)
{
Item item = collection.Take();
Console.WriteLine("Processed[{0}] : {1}", item.ID, item.Name);
//Thread.Sleep(100);
}
}
public List<Item> CreateListOfItems()
{
List<Item> lsItems = new List<Item>();
for (int i = 0; i <= 9999; i++)
{
lsItems.Add(new Item() { ID = i, Name = "Item[" + i + "]" });
}
return lsItems;
}
}
BlockCollection 实现(由于我们的环境是 .Net 3.5,我们不能在更高版本上使用库)。
public class BlockingQueue<T>
{
private readonly Queue<T> queue = new Queue<T>();
private readonly int MaxSize;
public bool closing;
public BlockingQueue(int maxSize) {
this.MaxSize = maxSize;
}
public void Add(T item)
{
lock(queue)
{
while(queue.Count >= this.MaxSize)
{
Monitor.Wait(queue);
}
queue.Enqueue(item);
if(queue.Count == 1)
{
Monitor.PulseAll(queue);
}
}
}
public T Take()
{
lock(queue)
{
while(queue.Count == 0)
{
Monitor.Wait(queue);
}
T item = queue.Dequeue();
if(queue.Count == MaxSize - 1)
{
Monitor.PulseAll(queue);
}
return item;
}
}
public void Close()
{
lock (queue)
{
closing = true;
Monitor.PulseAll(queue);
}
}
public bool TryDequeue(out T value)
{
lock (queue)
{
while (queue.Count == 0)
{
if (closing)
{
value = default(T);
return false;
}
Monitor.Wait(queue);
}
value = queue.Dequeue();
if (queue.Count == MaxSize - 1)
{
Monitor.PulseAll(queue);
}
return true;
}
}
}
最佳答案
您可以只检查所有工作线程的属性 IsAlive。看起来不是很清楚的代码,但它有效:
public void StartMonitor(object state)
{
List<Thread> producers = new List<Thread>();
List<Thread> consumers = new List<Thread>();
for (int i = 0; i < 1; i++)
{
producers.Add(new Thread(() => RunProducers(this.collection)));
}
//TODO: Start all producer threads...
for (int i = 0; i < 2; i++)
{
consumers.Add(new Thread(() => RunConsumers(this.collection)));
}
//TODO: Let Thread wait until all worker threads are done
List<Thread> to_check = new List<Thread>(producers);
to_check.AddRange(consumers);
while(true)
{
Thread.Sleep(50);
List<Thread> is_alive = new List<Thread>();
foreach(Thread t in to_check)
if(t.IsAlive)
is_alive.Add(t);
if(is_alive.Count == 0)
break;
to_check = is_alive;
}
//TODO: Dispose Threads
TimerThread.Change(5000, Timeout.Infinite);
}
或者,也许更好的方法:
private int[] _counter = new int[1];
private int Counter
{
get
{
lock (_counter) { return _counter[0]; }
}
set
{
lock (_counter) { _counter[0] = value; }
}
}
public void StartMonitor(object state)
{
List<Thread> producers = new List<Thread>();
List<Thread> consumers = new List<Thread>();
Counter = 0;
for (int i = 0; i < 1; i++)
{
producers.Add(new Thread(() => { Counter++; RunProducers(this.collection); Counter--; }));
}
//TODO: Start all producer threads...
for (int i = 0; i < 2; i++)
{
consumers.Add(new Thread(() => { Counter++; RunConsumers(this.collection); Counter--; }));
}
//TODO: Let Thread wait until all worker threads are done
List<Thread> to_check = new List<Thread>(producers);
to_check.AddRange(consumers);
while (Counter > 0)
Thread.Sleep(50);
//TODO: Dispose Threads
TimerThread.Change(5000, Timeout.Infinite);
}
为了避免使用 Sleep(),您可以使用 Barrier 类:
public void StartMonitor(object state)
{
List<Thread> producers = new List<Thread>();
List<Thread> consumers = new List<Thread>();
int producer_cnt = 1,
consumer_cnt = 2;
Barrier b = new Barrier(producer_cnt + consumer_cnt + 1);
try
{
for (int i = 0; i < 1; i++)
{
producers.Add(new Thread(() => { try { RunProducers(this.collection); } finally { b.SignalAndWait(); } }));
}
//TODO: Start all producer threads...
for (int i = 0; i < 2; i++)
{
consumers.Add(new Thread(() => { try { RunConsumers(this.collection); } finally { b.SignalAndWait(); } }));
}
//TODO: Let Thread wait until all worker threads are done
List<Thread> to_check = new List<Thread>(producers);
to_check.AddRange(consumers);
}
finally
{
b.SignalAndWait();
}
//TODO: Dispose Threads
TimerThread.Change(5000, Timeout.Infinite);
}
关于c# - 定时器+生产者消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53202228/
kafka的java客户端-生产者 生产者消息发送流程 发送原理 在消息发送的过程中,涉及俩个线程,main线程和sender线程,在main线程中创建一个双端队列RecordAccumulator。
我使用互斥体和条件编写了一个生产者/消费者程序。它使用全局 int 来生成和使用值。有 1 个消费者线程和多个生产者线程。 规则: 当值太小时,消费者会等待。 当值太大时,生产者就会等待。 我的问题是
我有兴趣发现当有多个产品和多个消费者时是否可以在不使用赋值的情况下解决生产者 - 消费者问题,即使用函数式编程风格?如何? Producer-consumer problem 谢谢 最佳答案 是的,您
单个进程中的两个不同线程可以通过读取和/或写入共享一个公共(public)内存位置。 通常,这种(有意的)共享是通过使用 lock 的原子操作来实现的。 x86 上的前缀,对于 lock前缀本身(即非
我正在尝试编写一个简单的生产者-消费者应用程序,在该应用程序中,我需要从文件中读取大块数据(可能很大),并且(出于简单测试目的)只需通过另一个线程将其写入另一个文件中即可。 我尝试了很多在线资源,但是
我已经为kafka(wurstmeister / kafka-docker)构建了一个docker镜像。在docker容器内部,我能够使用内置的shell脚本创建主题,生成消息并使用消息。现在,我正在
我正在尝试模拟关于多线程的生产者-消费者模型。 我们假设要遵守三个规则: 当桶装满产品时,生产者不能将产品添加到桶中。 当桶为空时,消费者无法从桶中获取产品。 生产和消费不能同时进行。换句话说,这两个
我有一个生成器应用程序,可以生成索引(将其存储在某些内存树数据结构中)。消费者应用程序将使用索引来搜索部分匹配。 我不希望消费者 UI 在生产者索引数据时必须阻塞(例如通过某些进度条)。基本上,如果用
我正在尝试为我遇到的排队问题找到解决方案。在典型的场景中,生产者将一些东西放入队列中,而消费者将其取出。如果我们有一个也消费的生产者和一个最初从队列中取出某些内容然后将某些内容(例如结果)放回到队列中
虽然以下是众所周知的话题,但我想请您提供意见。我写了一个小程序如下:所有生产者和消费者都排队。我不明白为什么会这样。什么场景下可以完全阻塞。 让我们考虑一下生产者/消费者正在等待数组上的锁,以及是什么
下面是我用于实现生产者-消费者问题的代码。使用 notifyAll() 一切正常,但是由于性能原因,我想用 notify() 替换所有出现的 notifyAll() >. 我发现通过将 notifyA
我有一个生产者-消费者的基本实现,如下所示: 我的问题是如何使线程数:x ~ y 来提高应用程序性能和负载平衡?有人有关键字或提示吗?预先感谢您! 最佳答案 您应该能够通过 Little's La
我编写了一个类“Producer”,它连续解析特定文件夹中的文件。解析的结果将存储在Consumer的队列中。 public class Producer extends Thread { p
我遇到“生产者 - 消费者任务”中可能出现死锁的问题。一切都应该按以下方式进行: 生产者应该生成 int[] 数组并将其添加到集合中 消费者应该获取这些数组,将它们放入第二个集合并在输出中打印 在 D
我正在为我的操作系统类(class)做一个 CPU 调度模拟器项目。该程序应包含两个线程:生产者线程和消费者线程。生产者线程包括在系统中生成进程的生成器和选择多个进程并将它们放入一个名为 Buffer
我想知道是否可以通过 AMQP 和 RabbitMQ 为生产者和消费者使用不同的语言? 例如:Java 用于生产者,python/php 用于消费者,还是反之? 最佳答案 是的,AMQP 与语言无关,
编辑:我有一个生产者类,它将一些数据发送到 SharedBuffer 类。该数据被添加到 ArrayList 中,限制设置为 100。将数据添加到所述列表中没有问题,但消费者类无法从列表中获取任何数据
我正在尝试在有界缓冲区中使用生产者/消费者线程。缓冲区长度为 5。我有 1 个互斥体和 2 个信号量,空信号量从缓冲区大小开始,满信号量从 0 开始。 当我在最后没有 sleep() 的情况下运行代码
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我用Java的LinkedBlockingDeque实现了生产者-消费者模式,但我遇到了一个问题,我有时想将一个项目(已经在队列中的某个位置)移动到队列的前面,以便更快地处理它。我永远不知道哪些已经排
我是一名优秀的程序员,十分优秀!