zoukankan      html  css  js  c++  java
  • 秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock (续)

    java 包实现了读写锁的操作:

    package com.multithread.readwritelock;
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReadWriteLock;
    import java.util.concurrent.locks.ReentrantReadWriteLock;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.multithread.main.ExampleInterface;
    import com.multithread.readwrite.WriteThread;
    
    public class ReadWriteLockExample extends ExampleInterface {
    
        public ReadWriteLock mReadwriteLock = new ReentrantReadWriteLock(false);
        public File mFile = null;
        public int mCash = 10000;
        public CountDownLatch mLatchDown = new CountDownLatch(6);
        private boolean mStopedFlag = false;
    
        @Override
        public void startDemo() {
            // TODO Auto-generated method stub
            mFile = new File("H:\Project\SST\123.txt");
            try {
    
                mFile.createNewFile();
                Executor mEcecutor = Executors.newFixedThreadPool(2 + 4);
                mEcecutor.execute(new Reader("Reader1"));
                mEcecutor.execute(new Reader("Reader2"));
                mEcecutor.execute(new Reader("Reader3"));
                mEcecutor.execute(new Reader("Reader4"));
                mEcecutor.execute(new Writer("Writer1", 2000));
                mEcecutor.execute(new Writer("Writer2", -3000));
    
                mLatchDown.await();
                System.out.println("[startDemo]" + "Demo down");
    
            } catch (IOException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public class Reader extends Thread {
    
            public String name = null;
            boolean flag = true;
            private int index = 0;
    
            public Reader(String name) {
                this.name = name;
            }
    
            @Override
            public void run() {
                while (flag) {
                    try {
                        mReadwriteLock.readLock().lock();
                        System.out.println("[Reader]" + name + "start");
                        Thread.sleep((long) (Math.random() * 100));
                        if (!mStopedFlag) {
                            System.out.println("[Reader]" + name + "get mcash now:"
                                    + mCash);
                        } else {
                            flag = false;
                        }
                        Thread.sleep((long) (Math.random() * 100));
                        System.out.println("[Reader]" + name + "down");
                        mReadwriteLock.readLock().unlock();
                        Thread.sleep((long) (Math.random() * 1000));
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
    
                }
                mLatchDown.countDown();
    
            }
    
        }
    
        public class Writer extends Thread {
    
            public String name = null;
            boolean flag = true;
            private int index = 0;
            private int cash = 0;
    
            public Writer(String name, int addcash) {
                this.name = name;
                this.cash = addcash;
            }
    
            @Override
            public void run() {
                System.out.println("[Writer]" + name + "start");
                while (flag) {
    
                    try {
                        mReadwriteLock.writeLock().lock();
                        System.out.println("[Writer]" + name + "start");
                        Thread.sleep((long) (Math.random() * 100));
                        int oldcash = mCash;
                        if (mCash <= 0) {
                            flag = false;
                            mStopedFlag = true;
                        } else {
                            mCash += cash;
                            System.out.println("[Writer]" + name
                                    + "operator cash old:" + oldcash + " To:"
                                    + mCash);
                        }
                        Thread.sleep((long) (Math.random() * 100));
                        System.out.println("[Writer]" + name + "down");
                        mReadwriteLock.writeLock().unlock();
                        Thread.sleep((long) (Math.random() * 1000));
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                }
                mLatchDown.countDown();
            }
    
        }
    
    }

    读写锁的特点就是:

    1.写者与读者是互斥的

    2.每个写者之间是互斥的

    3.读者之间可以同时进行

  • 相关阅读:
    UVA 1386
    疯狂Android演讲2 环境配置
    七内部排序算法汇总(插入排序、Shell排序、冒泡排序、请选择类别、、高速分拣合并排序、堆排序)
    【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识
    jQuery选择
    一个月操作总结
    C++易vector
    oracle rac 在完成安装错误。
    NginX issues HTTP 499 error after 60 seconds despite config. (PHP and AWS)
    解决Eclipse中文乱码的方法
  • 原文地址:https://www.cnblogs.com/deman/p/4113191.html
Copyright © 2011-2022 走看看