综合

img hippoz

[原创]C#编写的读者写者问题(自私的读者)

发表于2004/9/29 18:45:00  1142人阅读

// 自私读者的 读者写者问题
using System;
using System.Threading;
public class Book{
        private int[] pages;
        
        private object mutex = new Object();    // 控制对 readerCount 的访问
        private object db = new Object();       // 控制对 pages 的访问

        private int readerCount = 0;
        
        public Book(int numOfPages){
                pages = new int[numOfPages];
        }

        public int NumOfPages{
                get{
                        return pages.Length;
                }
        }

        public int this[int index]{
                get{
                        lock(mutex){
                                //检查是否为第一个读者
                                readerCount++;
                                if(readerCount == 1)    Monitor.Enter(db);
                        }
                        
                        int valueCopy = pages[index];   // 读取一个数据

                        Console.WriteLine( Thread.CurrentThread.Name + " 读了 第" + (index + 1) + "页,他读到的数据是" + valueCopy );

                        DisplayContent();

                        lock(mutex){
                                readerCount--;
                                if(readerCount == 0)    Monitor.Exit(db);
                        }

                        return valueCopy;
                }
                set{
                        lock(db){
                                pages[index] = value;
                                Console.WriteLine( Thread.CurrentThread.Name + "把" + value + "写到第" + (index + 1) + "页");
                                DisplayContent();
                        }
                }
        }
        
        private void DisplayContent(){
                Console.WriteLine("{0,-35}","书的内容是:");
                for(int i = 0;i < pages.Length;i++){
                        Console.Write("{0,-9}",pages[i]);
                }
                Console.WriteLine("/r/n");
        }
}

public class Reader{
        private Book book;
        private Random rand;
        
        public Reader(Book b,Random random){
                book = b;
                rand = random;
        }
        
        public void Read(){
                for (int i=0; i < 5; i++){
                        int p = rand.Next(0,book.NumOfPages);
                        int a = book[p];
                        Thread.Sleep(rand.Next(0,300));
                }
        }
}


public class Writer{
        private Book book;
        private Random rand;
        
        public Writer(Book b,Random random){
                book = b;
                rand = random;
        }
        
        public void Write(){
                for(int i = 0;i < 5;i++){
                        int p = rand.Next(0,book.NumOfPages);
                        book[p] = rand.Next(50,100);                    //写
                        Thread.Sleep(rand.Next(0,300));
                }
        }
}

public class Test{
        public static void Main(string[] args){
                Book book = new Book(5);
                Random random = new Random();
                
                string[] readerNames = {"猪八戒","唐僧","沙和尚"};
                string[] writerNames = {"观音菩萨","孙悟空","玉皇大帝"};

                Thread[] readerThreads = new Thread[3];
                Thread[] writerThreads = new Thread[3];

                for(int i = 0;i < 3;i++){
                        Reader reader = new Reader(book,random);
                        readerThreads[i] = new Thread(new ThreadStart(reader.Read));
                        readerThreads[i].Name = readerNames[i];
                        
                        Writer writer = new Writer(book,random);
                        writerThreads[i] = new Thread(new ThreadStart(writer.Write));
                        writerThreads[i].Name = writerNames[i];

                }
                
                for (int i=0; i<3; i++){
                        readerThreads[i].Start();
                        writerThreads[i].Start();
                }      
        }
}

阅读全文
0 0

相关文章推荐

img
取 消
img