生產者和消費者(.net實現)

發表於2014-06-16

        生產者和消費者,是多執行緒中的經典問題,聽過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; }
        }

    }
}
View Code

   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 = 0; i < 100; i++)
            {
                lock (info.LockObj)
                {
                    if (info.HasContent)
                        Monitor.Wait(info.LockObj);

                    info.Name = "Name" + i;
                    Thread.Sleep(500);
                    info.Content = "Content" + i;

                    Console.WriteLine("生產訊息:"+this.info.Name+","+this.info.Content);

                    info.HasContent = true;
                    Monitor.PulseAll(info.LockObj);
                }
            }
        }
    }
}
View Code

   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 = 0; i < 100; i++)
            {
                lock (info.LockObj)
                {
                    if (!info.HasContent)
                        Monitor.Wait(info.LockObj);

                    Thread.Sleep(500);
                    Console.WriteLine("消費訊息:"+this.info.Name+","+this.info.Content);
                    info.HasContent = false;
                    Monitor.PulseAll(info.LockObj);
                }
            }
        }
    }
}
View Code

   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();
        }
    }
}
View Code

 

相關文章