码迷,mamicode.com
首页 > 编程语言 > 详细

多线程--读者写者问题

时间:2015-07-05 09:42:52      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:多线程   读者写者   java   

解法一:
读者写者之间互斥(semaphore)
读者读者之间并行(如果有一个读者在读,后面的读者都可以读而不需要去申请许可)

public class ReaderAndWriter  
{
    private static  AtomicInteger reader = new AtomicInteger();
    private static  AtomicInteger blocked = new AtomicInteger();

    //用来对读者写者互斥
    private static Semaphore ws = new Semaphore(1,true);

    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
        ExecutorService executors = Executors.newFixedThreadPool(5);
        executors.execute(new Writerr());
        executors.execute(new Readerr(1));
        executors.execute(new Readerr(2));
        executors.execute(new Readerr(3));
        executors.execute(new Readerr(4));
        executors.shutdown();
    }
    static class Writerr implements Runnable
    {
        public void run(){
            try{
                while(true){
                    ws.acquire();
                    write();
                    //System.out.println("正在等待:" + ws.getQueueLength()+"-------"+blocked.get());
                    ws.release(blocked.get()==0 ? 1: blocked.get());
                    blocked.set(0);
                }
            }catch(InterruptedException ex){

            }
        }
        private static void write(){
            try{
                System.out.println("writing...");
                Thread.sleep((new Random().nextInt(2))*1000);
                System.out.println("writed!");
            }catch(InterruptedException ex){

            }
        }
    }
    static class Readerr implements Runnable
    {
        private int id;
        public Readerr(int id){
            this.id = id;
        }
        public void run(){
            try{
                while(true){
                    if(reader.get() == 0){
                        blocked.getAndIncrement();
                        ws.acquire();
                    }       
                    //以原子方式将当前值加 1
                    reader.getAndIncrement();   
                    read();
                    //以原子方式将当前值减 1
                    reader.getAndDecrement();
                    if(reader.get() == 0){
                        ws.release();
                    }
                    //出让时间,不与writer过度竞争
                    //System.out.println("["+id+"]出让时间");
                    Thread.sleep(6000);
                }
            }catch(InterruptedException ex){

            }
        }
        private void read(){
            try{
                System.out.println("["+id + "]reading...");
                Thread.sleep((new Random().nextInt(10))*1000);
                System.out.println("["+id+"]read!");
            }catch(InterruptedException ex){

            }
        }
    }
}

技术分享

解法二:
利用synchronized 加锁

public class ReaderAndWriter1 
{
    public static FILE file = new FILE();

    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
        ExecutorService executors = Executors.newFixedThreadPool(5);
        executors.execute(new Writerr());
        executors.execute(new Readerr(1));
        executors.execute(new Readerr(2));
        executors.execute(new Readerr(3));
        executors.execute(new Readerr(4));
        executors.shutdown();
    }
    static class Writerr implements Runnable
    {
        public void run(){
            while(true){
                file.write();
            }
        }

    }
    static class Readerr implements Runnable
    {
        private int id;
        public Readerr(int id){
            this.id = id;
        }
        public void run(){
            try{
                while(true){
                    file.read(id);
                    //出让时间,不与writer过度竞争
                    //System.out.println("["+id+"]出让时间");
                    Thread.sleep(6000);
                }
            }catch(InterruptedException ex){

            }
        }
    }
}

class FILE 
{
    //读者数
    private static AtomicInteger reader = new AtomicInteger(0);
    //写者数
    private static AtomicInteger writer = new AtomicInteger(0);
    //写文件
    public void write(){
        try{
            synchronized(reader){
                if(reader.get() > 0)
                {
                    reader.wait();
                }
            }
            synchronized(writer){
                writer.getAndIncrement();
                System.out.println("writing...");
                Thread.sleep((new Random().nextInt(2))*1000);
                System.out.println("writed!");
                writer.getAndDecrement();
                writer.notifyAll();
            }
        }catch(InterruptedException ex){

        }
    }
    //读文件
    public void read(int id){
        try{
            synchronized(writer){
                if(writer.get() > 0){
                    writer.wait();
                }
            }
            reader.getAndIncrement();
            System.out.println("["+id + "]reading...");
            Thread.sleep((new Random().nextInt(10))*1000);
            System.out.println("["+id+"]read!");
            reader.getAndDecrement();
            synchronized(reader){
                if(reader.get() == 0){
                    reader.notify();
                }
            }
        }catch(InterruptedException ex){

        }
    }
}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

多线程--读者写者问题

标签:多线程   读者写者   java   

原文地址:http://blog.csdn.net/havedream_one/article/details/46757399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!