生产者和消费者(.net实现)
生产者和消费者,是多线程中的经典问题,听过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实现)的更多相关文章
- 第3月第2天 find symbolicatecrash 生产者-消费者 ice 引用计数
1.linux find export find /Applications/Xcode.app/ -name symbolicatecrash -type f export DEVELOPER_DI ...
- LMAX Disruptor—多生产者多消费者中,消息复制分发的高性能实现
解决的问题 当我们有多个消息的生产者线程,一个消费者线程时,他们之间如何进行高并发.线程安全的协调? 很简单,用一个队列. 当我们有多个消息的生产者线程,多个消费者线程,并且每一条消息需要被所有的消费 ...
- JAVA并发框架之Semaphore实现生产者与消费者模型
分类: Java技术 锁和信号量(Semaphore)是实现多线程同步的两种常用的手段.信号量需要初始化一个许可值,许可值可以大于0,也可以小于0,也可以等于0. 如果大于0,表示 ...
- java 22 - 19 多线程之生产者和消费者的代码优化
在之前,是把生产者录入数据和消费者获取数据的所有代码都分别写在各自的类中. 这样不大好 这次把生产者和消费者部分关键代码都写入资源类中: package zl_Thread; public class ...
- java 22 - 16 多线程之生产者和消费者的问题
生产者和消费者问题的描述图 通过上图,我们可以发现: 生产者和消费者使用的都是同一个资源(肉包子) 所以,当使用线程的时候,这两类的锁也是同一把锁(为了避免出现线程安全问题) 例子:学生信息的录入和获 ...
- Java实现生产者和消费者
生产者和消费者问题是操作系统的经典问题,在实际工作中也常会用到,主要的难点在于协调生产者和消费者,因为生产者的个数和消费者的个数不确定,而生产者的生成速度与消费者的消费速度也不一样,同时还要实现生产者 ...
- 线程操作案例--生产者与消费者,Object类对线程的支持
本章目标 1)加深对线程同步的理解 2)了解Object类中对线程的支持方法. 实例 生产者不断生产,消费者不断消费产品. 生产者生产信息后将其放到一个区域中,之后消费者从区域中取出数据. 既然生产的 ...
- linux第13天 生产者与消费者
pthread_cond_t my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INIT ...
- java多线程中的生产者与消费者之等待唤醒机制@Version1.0
一.生产者消费者模式的学生类成员变量生产与消费demo,第一版1.等待唤醒: Object类中提供了三个方法: wait():等待 notify():唤醒单个线程 notify ...
- C# 线程(四):生产者和消费者
From : http://kb.cnblogs.com/page/42530/ 前面说过,每个线程都有自己的资源,但是代码区是共享的,即每个线程都可以执行相同的函数.这可能带来的问题就是几个线程同时 ...
随机推荐
- Apache Spark探秘:三种分布式部署方式比较
转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/ 目 ...
- 双倍边距bug
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- javascript_22_for_js控制div每五个换一行
2. 3. css: <style type="text/css"> div{height: 50px; width: 50px; background: #f1161 ...
- 30个实用的Linux find命令示例
除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易. 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令. 首先,在你 ...
- mybatis集成spring的事务管理
第一 创建一个测试实体 public class Order { private int id; private String orderName; public Order(String order ...
- 【转载】struct和typedef struct彻底明白了
分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...
- Codeforces Round #241 (Div. 2)->B. Art Union
B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- ural 1864
题意描述不清 而且还卡精度 ~~ #include <cstdio> #include <cstring> #include <iostream> using ...
- [转载]Spring Java Based Configuration
@Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...
- [转载]Spring Autowire自动装配介绍
转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...