线程同步介绍及 生产者消费者问题举例 C#版

internal class Program
{
//最多容纳50个橘子每个框
static readonly int MAX = 50;
//两个框
static List<ConcurrentQueue<Orange>> Queues =
new List<ConcurrentQueue<Orange>>();
//记录空闲的框
static List<int> QidxBags = new List<int>(); static int MaxO = 1000; //最多摘1000个橘子 static readonly object Sync = new object();
static readonly object Sync2 = new object();
//比起AutoResetEvent,可以唤起多个线程,如果说小孩一次拿多个橘子,而不是一个,
//这种方式比AutoResetEvent有优势,因为AutoResetEvent只唤醒一个线程。
static ManualResetEvent MResetEvent = new ManualResetEvent(false); static void Main(string[] args)
{
Queues.Add(new ConcurrentQueue<Orange>());
Queues.Add(new ConcurrentQueue<Orange>()); for (int i = 0; i < Queues.Count; i++)
{
QidxBags.Add(i);
}
TaskProduceAndComsummer();
Console.ReadKey();
} static int GetQueuesIdx()
{
int idx = -1; int count = QidxBags.Count; if (count > 0)
{
return count;
} return idx;
} static bool IsEmpty()
{
foreach (var item in Queues)
{
if (item.Count >0)
{
return false;
}
}
return true;
} static bool IsFull()
{
foreach (var item in Queues)
{
if (item.Count < MAX)
{
return false;
}
}
return true;
} static void TaskProduceAndComsummer()
{
for (int i = 0; i < 5; i++)
{
string name = "工人_" + (i + 1);
Task t = new Task(Produce, (object)(name)); t.Start();
} for (int i = 0; i < 3; i++)
{
string name = "小孩_" + (i + 1);
Task t = new Task(Consumer, (object)(name)); t.Start();
} }
static void Produce(object name)
{
while (true&&MaxO>0)
{
int count = -1;
int iPos = -1;
lock (Sync2)
{
count = GetQueuesIdx();
}
if (count > 0&&!IsFull())
{
bool refTaken = false; Monitor.Enter(Sync, ref refTaken);
bool isPut = false;
try
{ for (int i = 0; i < count; i++)
{ iPos = QidxBags[i];
var q = Queues[iPos]; if (q.Count < MAX)
{
QidxBags.Remove(iPos);
q.Enqueue(Orange.GetOrange());
MaxO -= 1;
Console.WriteLine(name + ":+摘了一个橘子,放入框【" + iPos + "】中");
Console.WriteLine("框一数量:{0},框二数量{1}", Queues[0].Count, Queues[1].Count);
isPut = true;
//唤醒小孩线程
MResetEvent.Set();
break;
} }
}
finally
{
if (refTaken)
{
if (iPos > -1)
{
QidxBags.Add(iPos);
} Monitor.Exit(Sync);
if (!isPut)
{
Console.WriteLine("满了");
} }
}
}
else
{
MResetEvent.WaitOne();
} }
} static void Consumer(object name)
{
while (true)
{
int count = GetQueuesIdx();
int iPos = -1;
if (count > 0&&!IsEmpty())
{
bool refTaken = false;
bool isPut = false;
Monitor.Enter(Sync, ref refTaken);
try
{
for (int i = 0; i < count; i++)
{ iPos = QidxBags[i];
var q = Queues[iPos]; if (q.Count >0)
{
QidxBags.Remove(iPos);
Orange o = null;
q.TryDequeue(out o);
Console.WriteLine(name + ":+拿了一个橘子,从框【" + iPos + "】中");
Console.WriteLine("框一数量:{0},框二数量{1}", Queues[0].Count, Queues[1].Count);
isPut = true;
//框有容量了,可以放了,所以唤醒被阻塞得工人线程
MResetEvent.Set();
break;
} }
}
finally
{
if (refTaken)
{
if (iPos > -1)
{
QidxBags.Add(iPos);
}
Monitor.Exit(Sync);
if (!isPut)
{ Console.WriteLine("都空了"); } }
}
}
else
{
MResetEvent.WaitOne();//阻塞
}
}
}
} public class Orange
{
public static Orange GetOrange()
{
Random rand = new Random();
int t = rand.Next(10, 20);
Thread.Sleep(t);
return new Orange();
}
}
部分结果:

线程同步介绍及 生产者消费者问题举例 C#版的更多相关文章
- 第三节: List类型的介绍、生产者消费者模式、发布订阅模式
一. List类型基础 1.介绍 它是一个双向链表,支持左进.左出.右进.右出,所以它即可以充当队列使用,也可以充当栈使用. (1). 队列:先进先出, 可以利用List左进右出,或者右进左出(Lis ...
- java线程之多个生产者消费者
温故一下上一节所学习的生产者消费者代码: 两个线程时: 通过标志位flag的if判断和同步函数互斥较好解决两个线程,一个生产者.一个消费者交替执行的功能 类名:ProducterConsumerDem ...
- python进阶:Python进程、线程、队列、生产者/消费者模式、协程
一.进程和线程的基本理解 1.进程 程序是由指令和数据组成的,编译为二进制格式后在硬盘存储,程序启动的过程是将二进制数据加载进内存,这个启动了的程序就称作进程(可简单理解为进行中的程序).例如打开一个 ...
- java线程之多个生产者消费者2.0
上一节中,通过while和notifyAll解决了多个生产者,消费者对共享资源的访问问题,现在开始升级 但是,仍然有改进之处,主要体现在两点: 1)使用新版本1.5开始后的锁Lock解决,目的将其全部 ...
- 基于线程池的线程管理(BlockingQueue生产者消费者方式)实例
1.线程池管理类: public class ThreadPoolManager { private static ThreadPoolManager instance = new ThreadPoo ...
- 线程_FIFO队列实现生产者消费者
import threading # 导入线程库 import time from queue import Queue # 队列 class Producer(threading.Thread): ...
- Ruby:线程实现经典的生产者消费者问题
运行结果: ProAndCon 0 produced 1 produced consumed 0 2 produced 3 produced consumed 1 consumed 2 consume ...
- Python之多线程:线程互斥与线程同步
一.锁在多线程中的使用:线程互斥 lock = threading.Lock()#创建一个锁对象 1.with lock: pass 和进程使用的方式相同 2.控制线程结束的时间 通过一个全局变量 ...
- 使用Win32 API实现生产者消费者线程同步
使用win32 API创建线程,创建信号量用于线程的同步 创建信号量 语法例如以下 HANDLE semophore; semophore = CreateSemaphore(lpSemaphoreA ...
随机推荐
- Hibernate使用distinct返回不重复的数据,使用group by 进行分组
//distinct使用 public List<String> distinctDutyDate() { String hql="select distinct(dutyDat ...
- 【九度OJ】题目1138:进制转换 解题报告
[九度OJ]题目1138:进制转换 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1138 题目描述: 将一个长度最多为30 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- lightoj 1102 - Problem Makes Problem
1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...
- DAG-GNN: DAG Structure Learning with Graph Neural Networks
目录 概 主要内容 代码 Yu Y., Chen J., Gao T. and Yu M. DAG-GNN: DAG structure learning with graph neural netw ...
- 基于Java swing+mysql+eclipse的【水电费管理系统】
本项目为前几天收费帮学妹做的一个项目,Java swing项目,在工作环境中基本使用不到,但是很多学校把这个当做编程入门的项目来做,故分享出本项目供初学者参考. CSDN9.9赞助下载: https: ...
- docker学习:docker---centos安装
查看目标镜像 docker search centos 拉取镜像 docker pull centos 查看镜像 docker images 启动镜像 docker run -itd --privil ...
- Java高效开发-fiddler抓包工具
1.简介 Fiddler是最常用的抓包工具之一,只要打开之后就能够实现数据包抓取,关闭之后会自动取消代理,非常方便本地调试 2.下载 阿里云盘地址:https://www.aliyundrive.co ...
- SpringCloud集成Security安全(Eureka注册中心)
1.说明 为了保护注册中心的服务安全, 避免恶意服务注册到Eureka, 需要对Eureka Server进行安全保护, 本文基于Spring Security方案, 为Eureka Server增加 ...