• 大小: 194KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签:

资源简介

生产者消费者为模型的多线程编程的c#实现,原创包括代码和程序,算法核心已经封装。可重用。

资源截图

代码片段和文件信息

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


class Threadobject//具有模拟生产及消费功能的一个类
{
    Int32 id;//用户id的申明
    Int32 max;//最大等待队列长度的申明
    List list = new List();//为便于控制使临界资源尽量少采用了framework2.0中新添加的list<>类作为等待队列

    public Threadobject(Int32 x)//构造函数(便于对对象的初始化)
    {
        this.id = 0;
        this.max = x;
        this.list.Clear();
    }

    public void customer()//具有模拟生产者功能的方法
    {
        Console.WriteLine(Thread.CurrentThread.Name + “已经启动!“);
        Console.WriteLine(“_____________________________________________“);//输出提示叫号机启动
        while (true)//用while循环使生产者线程不会推出一直执(生产)行下去
        {
            Random producetime = new Random();//随机数声名
            Int32 time;
            lock (list)//锁住list对象
            {
                if (list.Count < max)//判断等待服务队列是否满
                {
                    Monitor.Enter(this);//监视id
                    id = id + 1;//id加一
                    list.Add(id);//将id add到list中(即一个id=一个客户)
                    Monitor .Exit (this);//推出监视
                    time = producetime.Next(1000 5000);//产生一个随机数阻塞生产线程相应的时间以模拟生产
                    Thread.Sleep(time);
                    Console.WriteLine(““);
                    Console.WriteLine(Thread.CurrentThread.Name + “ 已经产生 “ + id + “号客户!用时“ + time + “毫秒“+“ 队列中现有“+list .Count +“个客户!“);
                    Console.WriteLine(“_____________________________________________“);//生产提示
                }
                else
                {
                    Console.WriteLine(““);
                    Console.WriteLine(“_____________________________________________“);
                    Console.WriteLine(“顾客缓冲曲队列已满! “ + Thread.CurrentThread.Name + “ 被阻塞!“);
                    Console.WriteLine(“等待服务窗口接待客户!“);
                    Console.WriteLine(“_____________________________________________“);
                    Console.WriteLine(““);//阻塞提示
                    Thread.CurrentThread.Priority = ThreadPriority.Lowest;//将当前线程的优先级设置为最低以便以往阻塞得线程先得到处理机
                    Monitor.Wait(list);//线程进入阻塞队列
                }
                Monitor.Pulse(list);//唤醒阻塞的消费者线程
            }
        }
    }

    public void server()//具有模拟消费者功能的方法
    {
        Console.WriteLine(Thread.CurrentThread.Name + “ 已经启动!“);
        Console.WriteLine(“*********************************************“);//输出提示服务窗口启动
        while (true)
        {
            Random producetime = new Random();//随机数声名
            Int32 timenumber;
            lock (list)//锁住list对象
            {
                if (list.Count > 0)//判断等待服务队列是否为空
                {
                    number=list[0];//便于显示接受服务的客户
                    list.RemoveAt(0);//接受服务后移出队首的等待客户
                    time = producetime.Next(5000 6000);//产生一个随机数阻塞生产线程相应的时间以模拟生产
                    Thread.Sleep(time);
                    Console.WriteLine(““);
                    Console.WriteLine(Thread.Cur

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        922  2007-05-18 18:56  生产——消费.sln

     文件       1525  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.application

     文件      20480  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.exe

     文件       2185  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.exe.manifest

     文件      15872  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb

     文件       5266  2007-05-19 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish\ConsoleApplication1.application

     文件      20480  2007-05-19 13:18  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish\ConsoleApplication1_1_0_0_2\ConsoleApplication1.exe.deploy

     文件       5862  2007-05-19 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish\ConsoleApplication1_1_0_0_2\ConsoleApplication1.exe.manifest

     文件     423872  2007-05-19 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish\setup.exe

     文件       1525  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.application

     文件       5632  2005-12-08 14:51  ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe

     文件       2185  2007-10-21 13:37  ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest

     文件       3216  2007-05-18 19:10  ConsoleApplication1\cus-ser.csproj

     文件        536  2007-05-19 13:38  ConsoleApplication1\cus-ser.csproj.user

     文件       1676  2007-05-18 19:04  ConsoleApplication1\cus-ser_TemporaryKey.pfx

     文件        182  2007-05-18 18:56  ConsoleApplication1\obj\ConsoleApplication1.csproj.FileList.txt

     文件        356  2008-03-04 19:43  ConsoleApplication1\obj\cus-ser.csproj.FileList.txt

     文件       1525  2007-10-21 13:37  ConsoleApplication1\obj\Debug\ConsoleApplication1.application

     文件      20480  2007-10-21 13:37  ConsoleApplication1\obj\Debug\ConsoleApplication1.exe

     文件       2185  2007-10-21 13:37  ConsoleApplication1\obj\Debug\ConsoleApplication1.exe.manifest

     文件      15872  2007-10-21 13:37  ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb

     文件       6483  2007-09-08 11:24  ConsoleApplication1\Program.cs

     文件       1185  2007-05-18 14:04  ConsoleApplication1\Properties\AssemblyInfo.cs

     目录          0  2010-06-19 18:10  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish\ConsoleApplication1_1_0_0_2

     目录          0  2010-06-19 18:10  ConsoleApplication1\bin\Debug\ConsoleApplication1.publish

     目录          0  2009-10-07 17:57  ConsoleApplication1\obj\Debug\TempPE

     目录          0  2010-06-19 18:10  ConsoleApplication1\bin\Debug

     目录          0  2010-06-19 18:10  ConsoleApplication1\obj\Debug

     目录          0  2010-06-19 18:10  ConsoleApplication1\bin

     目录          0  2010-06-19 18:10  ConsoleApplication1\obj

............此处省略5个文件信息

评论

共有 条评论

相关资源