生产者和消费者,是多线程中的经典问题,听过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();
}
}
}

生产者和消费者(.net实现)的更多相关文章

  1. 第3月第2天 find symbolicatecrash 生产者-消费者 ice 引用计数

    1.linux find export find /Applications/Xcode.app/ -name symbolicatecrash -type f export DEVELOPER_DI ...

  2. LMAX Disruptor—多生产者多消费者中,消息复制分发的高性能实现

    解决的问题 当我们有多个消息的生产者线程,一个消费者线程时,他们之间如何进行高并发.线程安全的协调? 很简单,用一个队列. 当我们有多个消息的生产者线程,多个消费者线程,并且每一条消息需要被所有的消费 ...

  3. JAVA并发框架之Semaphore实现生产者与消费者模型

    分类: Java技术      锁和信号量(Semaphore)是实现多线程同步的两种常用的手段.信号量需要初始化一个许可值,许可值可以大于0,也可以小于0,也可以等于0.      如果大于0,表示 ...

  4. java 22 - 19 多线程之生产者和消费者的代码优化

    在之前,是把生产者录入数据和消费者获取数据的所有代码都分别写在各自的类中. 这样不大好 这次把生产者和消费者部分关键代码都写入资源类中: package zl_Thread; public class ...

  5. java 22 - 16 多线程之生产者和消费者的问题

    生产者和消费者问题的描述图 通过上图,我们可以发现: 生产者和消费者使用的都是同一个资源(肉包子) 所以,当使用线程的时候,这两类的锁也是同一把锁(为了避免出现线程安全问题) 例子:学生信息的录入和获 ...

  6. Java实现生产者和消费者

    生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...

  7. 线程操作案例--生产者与消费者,Object类对线程的支持

    本章目标 1)加深对线程同步的理解 2)了解Object类中对线程的支持方法. 实例 生产者不断生产,消费者不断消费产品. 生产者生产信息后将其放到一个区域中,之后消费者从区域中取出数据. 既然生产的 ...

  8. linux第13天 生产者与消费者

    pthread_cond_t   my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INIT ...

  9. java多线程中的生产者与消费者之等待唤醒机制@Version1.0

    一.生产者消费者模式的学生类成员变量生产与消费demo,第一版1.等待唤醒:    Object类中提供了三个方法:    wait():等待    notify():唤醒单个线程    notify ...

  10. C# 线程(四):生产者和消费者

    From : http://kb.cnblogs.com/page/42530/ 前面说过,每个线程都有自己的资源,但是代码区是共享的,即每个线程都可以执行相同的函数.这可能带来的问题就是几个线程同时 ...

随机推荐

  1. System.IO.StreamWriter

    string path = @"D:\a.txt"; System.IO.StreamWriter swOut = new System.IO.StreamWriter(path, ...

  2. MyEclipse 2015 Stable 1.0下载安装破解日志

    前言 这2天下载了许多myeclipse版本,基本上是14/15版本的,各种破解均告以失败,这次下载了贴吧一个吧友提供的版本,现已破解.破解结果现不好说--目前已装SVN,根据经验,只有等待一定时间验 ...

  3. powerdesigner使用之——从“概念模型”到“物理模型”

    现实问题在计算机上的解决,需要我们从现实问题中抽象出实体模型,然后再将实体模型对应到数据库关系表中. 例如,我们在思考学生选课,这件事情上,实体模型就是“学生”和“课程”两个 此时,我们使用power ...

  4. BestCoder Round #2

    TIANKENG’s restaurant http://acm.hdu.edu.cn/showproblem.php?pid=4883 竟然暴力1.44*10^7  还要*T=100  竟然过了 # ...

  5. SQL Server 之 解锁

    下图,制作了一个可以维持1分钟的表锁: 下图,可以查询出被锁的表,其中 spid 是锁定表的进程ID(也是 session_id): 可以通过 select connect_time from sys ...

  6. C++字符串分割

    //字符串分割函数 std::vector<std::string> split(std::string str,std::string pattern) { std::string::s ...

  7. PHP读取xml方法讲解

    一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...

  8. [转]CentOS 5.5下FTP安装及配置

    一.FTP的安装 1.检测是否安装了FTP : [root@localhost ~]# rpm -q vsftpd vsftpd-2.0.5-16.el5_5.1 否则显示:[root@localho ...

  9. 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel

    在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...

  10. Unity3d Detect NetState

    public static bool HasConnection() { System.Net.WebClient client; System.IO.Stream stream; try { usi ...