⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boundedbuffer.cs

📁 jsp+javabean+servlet实现的在线书店
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace IM
{
    public class BoundedBuffer
    {
        const int BufferSize = 10;
        object synchObject = new object();
        AutoResetEvent notFull=new AutoResetEvent(false);
        AutoResetEvent notEmpty=new AutoResetEvent(false);
        object[] buffer = new object[BufferSize];
        int putPoint=0, takePoint=0,count=0;
        public void put(object o)
        {
            bool full = false;            
            lock (synchObject)
            {
                if (count == BufferSize)
                    full = true;
                Console.WriteLine("put: count="+count);
            }
            if (full)
            {
                Console.WriteLine("put: waiting for notFull ...");
                notFull.WaitOne();
                Console.WriteLine("put: get notFull signal ...");
            }
            lock (synchObject)
            {
                count++;
                buffer[putPoint] = o;
                putPoint = (putPoint + 1) % BufferSize;
            }
            notEmpty.Set();
        }
        public object take()
        {
            bool empty = false;
            lock (synchObject)
            {
                if (count == 0)
                    empty = true;
                Console.WriteLine("take: count=" + count);
            }
            if (empty)
            {
                Console.WriteLine("take: waiting for notEmpty ...");
                notEmpty.WaitOne();
                Console.WriteLine("take: get notEmpty signal ...");
            }
            object result;
            lock (synchObject)
            {
                count--;
                result = buffer[takePoint];
                takePoint = (takePoint + 1) % BufferSize;
            }
            notFull.Set();
            return result;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -