posts - 124,  comments - 29,  trackbacks - 0

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

namespace MulThead
{
    class signel
    {
        public Semaphore singleRead;
        public Semaphore singleWrite;
        public Semaphore singleMutex;
        private int size;

        public signel(int size)
        {
            this.size = size;
            singleRead = new Semaphore(0,size);         //一共size个read资源,目前有0个可用
            singleWrite = new Semaphore(size, size);    //一共size个write资源,目前有size个可用
            singleMutex = new Semaphore(1, 1);
        }
    }
    class Reader
    {
        private signel sgl;
        private Queue<int> que;
        private Form1 f1;
        //保存信号量和队列的一个引用
        public Reader(signel sgl, Queue<int> que,Form1 f1)
        {
            this.sgl = sgl;
            this.que = que;
            this.f1 = f1;
        }
        public void Process()
        {
            int readvalue;
            while(true)
            {
                sgl.singleRead.WaitOne();
                sgl.singleMutex.WaitOne();
                readvalue = que.Dequeue();
                f1.txtReader.Text += readvalue.ToString();
                f1.txtReader.Text += " ";
                sgl.singleMutex.Release();
                sgl.singleWrite.Release();     //释放写资源
            }
        }
       
    }
    class Writer
    {
        private signel sgl;
        private Queue<int> que;
        private Form1 f1;

        public Writer(signel sgl, Queue<int> que,Form1 f1)
        {
            this.sgl = sgl;
            this.que = que;
            this.f1 = f1;
        }
        public void Process()
        {

            int i = 0;
            while(i < 50)
            {
                i++;
                sgl.singleWrite.WaitOne();
                sgl.singleMutex.WaitOne();
                f1.txtWriter.Text += i.ToString();
                f1.txtWriter.Text += " ";
                que.Enqueue(i);
                sgl.singleMutex.Release();
                sgl.singleRead.Release();         //释放读资源
            }
        }

    }
}






        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;  //这样在不是创建该控件(此form里的控件)的线程里也可以用这个控件了
            //信号量
            signel sgl = new signel(10);

            //新建一个队列缓冲区
            Queue<int> que = new Queue<int>(10);
            Reader readerThread = new Reader(sgl,que,this);
            Writer writerThread = new Writer(sgl,que,this);
            //开启两个线程
            Thread tRead = new Thread(readerThread.Process);
            Thread tWrite= new Thread(writerThread.Process);
            tRead.Start();
            tWrite.Start();
        }

posted on 2008-09-10 13:07 天书 阅读(555) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜