首页 技术 正文
技术 2022年11月21日
0 收藏 882 点赞 3,255 浏览 1894 个字

生产者和消费者,是多线程中的经典问题,听过java方面的这个问题的培训,闲暇时用.net实现了这

个问题。在此实现的是,生产一个消息后,消费一个消息,再生产一个消息,循环往复。

 1.消息代码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ProducerAndConsumer
{
public class Info
{
private String name; public String Name
{
get { return name; }
set { name = value; }
}
private String content; public String Content
{
get { return content; }
set { content = value; }
} private Boolean hasContent; public Boolean HasContent
{
get { return hasContent; }
set { hasContent = value; }
}
private Object lockObj = new Object(); public Object LockObj
{
get { return lockObj; }
} }
}

   2.生产者代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ProducerAndConsumer
{
public class Producer
{
private Info info; public Producer(Info info)
{
this.info = info;
} public void Produce()
{
for (int i = ; i < ; i++)
{
lock (info.LockObj)
{
if (info.HasContent)
Monitor.Wait(info.LockObj); info.Name = "Name" + i;
Thread.Sleep();
info.Content = "Content" + i; Console.WriteLine("生产消息:"+this.info.Name+","+this.info.Content); info.HasContent = true;
Monitor.PulseAll(info.LockObj);
}
}
}
}
}

   3.消费者代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ProducerAndConsumer
{
public class Consumer
{
private Info info; public Consumer(Info info)
{
this.info = info;
} public void Consume()
{
for (int i = ; i < ; i++)
{
lock (info.LockObj)
{
if (!info.HasContent)
Monitor.Wait(info.LockObj); Thread.Sleep();
Console.WriteLine("消费消息:"+this.info.Name+","+this.info.Content);
info.HasContent = false;
Monitor.PulseAll(info.LockObj);
}
}
}
}
}

4.Main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ProducerAndConsumer
{
class Program
{
static void Main(string[] args)
{
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info); Thread th1 = new Thread(new ThreadStart(pro.Produce));
Thread th2 = new Thread(new ThreadStart(con.Consume)); th1.Start();
th2.Start(); th1.Join();
th2.Join();
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,953
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,478
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773