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.读者之间可以同时进行

  • 相关阅读:
    NodeJS学习笔记之Connect中间件模块(一)
    XML CDATA节点
    6个强大的AngularJS扩展应用
    java使用poi读取ppt文件和poi读取excel、word示例
    Java log4j详细教程
    JAVA8 十大新特性详解
    Express 路由
    基于 Node.js 平台,快速、开放、极简的 web 开发框架。
    Node.js 手册查询-1-核心模块方法
    Node.js 手册查询-2-MongoDB数据库方法
  • 原文地址:https://www.cnblogs.com/deman/p/4113191.html
Copyright © 2011-2022 走看看