生产者—消费者

本文通过C#代码示例介绍了线程同步机制及生产者消费者模式的应用。使用Mutex和AutoResetEvent来实现线程间的同步,确保数据的安全访问,并通过生产者消费者模式演示了如何在多线程环境中高效地进行资源分配。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;

namespace threading
{
//public class Goods
//{
// private string name;
// public string Name
// {
// get { return name; }
// set { name = value; }
// }

// private int count;
// public int Count
// {
// get { return count; }
// set { count = value; }
// }

// public override string ToString()
// {
// return "物品名称:" + name;
// }
//}

//public class GoodsPage
//{
// List<Goods> container = new List<Goods>();//得到一个容器
// public static Mutex mutex = new Mutex();

// public AutoResetEvent autoresetevent = new AutoResetEvent(false);
// private int count;

// public int Count
// {
// get { return count; }
// set { count = value; }
// }

// object goodlock = new object();

// public GoodsPage()
// {
// this.Count = 1;
// }

// public bool add(string name, int i)
// {
// if (this.container.Count < 10)
// {
// mutex.WaitOne();
// Console.WriteLine("增加线程获得锁");
// Goods goods = new Goods();
// goods.Name = name;
// goods.Count = this.Count++;
// container.Add(goods);
// Console.WriteLine("生产者{0}生产了物品:{1}", i, goods.ToString() + goods.Count);

// mutex.ReleaseMutex();
// Console.WriteLine("增加线程释放锁");
// return true;
// }
// else
// {
// Console.WriteLine("物品已满,请等待顾客消费!");
// return false;
// }
// }

// public bool del(string no)
// {

// if (this.container.Count > 0)
// {
// mutex.WaitOne();
// Console.WriteLine("删除线程获得锁");
// Goods gd = container[0];
// container.RemoveAt(0);
// Console.WriteLine(" 消费者{0}消费的物品是:{1}", no.ToString(), gd.ToString() + gd.Count);

// mutex.ReleaseMutex();
// Console.WriteLine("删除线程释放锁");
// return true;
// }
// else
// {

// Console.WriteLine("物品已空!需要生产了!");
// return false;

// }
// }


//}
//public class Producer
// {
// GoodsPage gp = null;

// public Producer(GoodsPage _gp)
// {
// gp = _gp;
// }

// public bool Product(string name, int i)
// {
// return gp.add(name, i);
// }
// }

//public class Consumer
// {
// GoodsPage gp = null;
// public Consumer(GoodsPage _gp)
// {
// gp = _gp;
// }

// public bool Consumption(object i)
// {
// return gp.del(i.ToString());
// }
// }

//class Program
// {

// Producer producer = null;
// Consumer consumer = null;
// GoodsPage gp = new GoodsPage();

// public void TProduct(object i)
// {
// producer = new Producer(gp);
// while (true)
// {
// producer.Product("冰箱", Convert.ToInt32(i));
// Thread.Sleep(1000);
// }
// }

// public void TConsumption(object i)
// {
// consumer = new Consumer(gp);
// while (true)
// {
// consumer.Consumption(i);
// Thread.Sleep(1000);
// }
// }

// static void Main(string[] args)
// {
// Program p = new Program();
// new Thread(new ParameterizedThreadStart(p.TProduct)).Start(1);

// new Thread(new ParameterizedThreadStart(p.TConsumption)).Start(1);

// GoodsPage gp1 = new GoodsPage();

// Console.Read();
// }



//}

 

}

 

转载于:https://www.cnblogs.com/pcc5201314/p/5044221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值