C# 生产者和消费者问题使用Monitor同步
2013-12-10 10:33
363 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p.run).Start();
new Thread(c.run).Start();
}
}
public class Cake
{
int id;
public Cake(int id)
{
this.id = id;
}
public String toString()
{
return "Cake" + id;
}
}
public class SyncStack
{
int index = 0;
Cake[] arrWT = new Cake[4];
public void push(Cake wt)
{
try
{
Monitor.Enter(this);
while (index == arrWT.Length)
{
try
{
Console.WriteLine("已经生产满了!");
Monitor.Wait(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Monitor.Pulse(this);
arrWT[index] = wt;
index++;
}
finally
{
Monitor.Exit(this);
}
}
public Cake pop()
{
try
{
Monitor.Enter(this);
while (index == 0)
{
try
{
Console.WriteLine("已经消费空了!");
Monitor.Wait(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Monitor.Pulse(this);
index--;
return arrWT[index];
}
finally
{
Monitor.Exit(this);
}
}
}
public class Producer
{
private bool flag = true;
private SyncStack m_syncStack = null;
public Producer(SyncStack ss)
{
this.m_syncStack = ss;
}
public void run()
{
int i = 0;
while (flag)
{
Cake wt = new Cake(i);
i++;
m_syncStack.push(wt);
Console.WriteLine("生产了:" + wt.toString());
}
}
public void Shutdown()
{
flag = false;
}
}
public class Consumer
{
private bool flag = true;
private SyncStack m_syncStack = null;
public Consumer(SyncStack ss)
{
this.m_syncStack = ss;
}
public void run()
{
while (flag)
{
Cake wt = m_syncStack.pop();
Console.WriteLine("消费了:" + wt.toString());
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public void Shutdown()
{
flag = false;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p.run).Start();
new Thread(c.run).Start();
}
}
public class Cake
{
int id;
public Cake(int id)
{
this.id = id;
}
public String toString()
{
return "Cake" + id;
}
}
public class SyncStack
{
int index = 0;
Cake[] arrWT = new Cake[4];
public void push(Cake wt)
{
try
{
Monitor.Enter(this);
while (index == arrWT.Length)
{
try
{
Console.WriteLine("已经生产满了!");
Monitor.Wait(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Monitor.Pulse(this);
arrWT[index] = wt;
index++;
}
finally
{
Monitor.Exit(this);
}
}
public Cake pop()
{
try
{
Monitor.Enter(this);
while (index == 0)
{
try
{
Console.WriteLine("已经消费空了!");
Monitor.Wait(this);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Monitor.Pulse(this);
index--;
return arrWT[index];
}
finally
{
Monitor.Exit(this);
}
}
}
public class Producer
{
private bool flag = true;
private SyncStack m_syncStack = null;
public Producer(SyncStack ss)
{
this.m_syncStack = ss;
}
public void run()
{
int i = 0;
while (flag)
{
Cake wt = new Cake(i);
i++;
m_syncStack.push(wt);
Console.WriteLine("生产了:" + wt.toString());
}
}
public void Shutdown()
{
flag = false;
}
}
public class Consumer
{
private bool flag = true;
private SyncStack m_syncStack = null;
public Consumer(SyncStack ss)
{
this.m_syncStack = ss;
}
public void run()
{
while (flag)
{
Cake wt = m_syncStack.pop();
Console.WriteLine("消费了:" + wt.toString());
try
{
Thread.Sleep(2000);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public void Shutdown()
{
flag = false;
}
}
}
相关文章推荐
- C#编写的多生产者多消费者同步问题
- [原创]C#编写的多生产者多消费者同步问题
- Java并发编程-07-在同步代码中使用条件--生产者-消费者问题
- C#编写的多生产者多消费者同步问题
- 用C#线程技术模拟“生产者-消费者”经典进程同步问题
- C#编写的多生产者多消费者同步问题
- 使用ReentrantLock模拟简单生产者和消费者问题
- Java Monitor Object在生产者消费者问题中的应用
- java生产者消费者同步问题
- Linux下生产者消费者问题详细分析(操作系统期中考试论文---并发程序的同步和互斥)
- 进程间同步互斥经典问题(一)生产者-消费者问题
- 使用Lock来实现生产者和消费者问题
- 进程同步---生产者与消费者问题以及进程同步机制--信号量及P、V操作
- 生产者消费者问题(C#)
- 《Lua程序设计》9.2 管道(pipe)与过滤器(filter) 包含使用协同函数实现“生产者——消费者”问题的实例代码
- 资源同步与互斥问题(生产者与消费者)
- 并发与同步、信号量与管程、生产者消费者问题
- java 生产者消费者问题以及线程池的使用
- c#中生产者消费者问题
- C#生产者与消费者问题(二)