乐趣区

java并发编程学习之ReentrantReadWriteLock

ReentrantReadWriteLock,读写锁,适用于读多写少的情况,允许多个线程进行读,但是有写的进程的时候,就阻塞。

示例

public class ReadWriteLockDemo {ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    Lock readLock = readWriteLock.readLock();
    Lock writeLock = readWriteLock.writeLock();
    int num = 0;
    int num2 = 0;

    public void readNum() {readLock.lock();
        try {Thread.sleep(5);
        } catch (InterruptedException e) {e.printStackTrace();
        } finally {readLock.unlock();
        }
    }

    public void writeNum() {writeLock.lock();
        try {
            num++;
            Thread.sleep(10);
        } catch (InterruptedException e) {e.printStackTrace();
        } finally {writeLock.unlock();
        }
    }

    public synchronized void readNum2() {
        try {Thread.sleep(5);
        } catch (InterruptedException e) {e.printStackTrace();
        }
    }

    public void writeNum2() {
        try {
            num2++;
            Thread.sleep(10);
        } catch (InterruptedException e) {e.printStackTrace();
        }
    }

    static class ReadThread extends Thread {
        ReadWriteLockDemo readWriteLockDemo;

        public ReadThread(ReadWriteLockDemo readWriteLockDemo) {this.readWriteLockDemo = readWriteLockDemo;}

        @Override
        public void run() {long start = System.currentTimeMillis();
            for (int i = 0; i < 100; i++) {readWriteLockDemo.readNum();
            }
            System.out.println("ReadThread 耗时:" + (System.currentTimeMillis() - start));
        }
    }

    static class WriteThread extends Thread {
        ReadWriteLockDemo readWriteLockDemo;

        public WriteThread(ReadWriteLockDemo readWriteLockDemo) {this.readWriteLockDemo = readWriteLockDemo;}

        @Override
        public void run() {long start = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {readWriteLockDemo.writeNum();
            }
            System.out.println("WriteThread 耗时:" + (System.currentTimeMillis() - start));
        }
    }

    static class ReadThread2 extends Thread {
        ReadWriteLockDemo readWriteLockDemo;

        public ReadThread2(ReadWriteLockDemo readWriteLockDemo) {this.readWriteLockDemo = readWriteLockDemo;}

        @Override
        public void run() {long start = System.currentTimeMillis();
            for (int i = 0; i < 100; i++) {readWriteLockDemo.readNum2();
            }
            System.out.println("ReadThread2 耗时:" + (System.currentTimeMillis() - start));
        }
    }

    static class WriteThread2 extends Thread {
        ReadWriteLockDemo readWriteLockDemo;

        public WriteThread2(ReadWriteLockDemo readWriteLockDemo) {this.readWriteLockDemo = readWriteLockDemo;}

        @Override
        public void run() {long start = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {readWriteLockDemo.writeNum2();
            }
            System.out.println("WriteThread2 耗时:" + (System.currentTimeMillis() - start));
        }
    }

    public static void main(String[] args) {ReadWriteLockDemo demo = new ReadWriteLockDemo();
        for (int i = 0; i < 10; i++) {for (int j = 0; j < 5; j++) {new ReadThread(demo).start();
                //   new ReadThread2(demo).start();}
              new WriteThread(demo).start();
            //   new WriteThread2(demo).start();}
    }
}

运行两个不同的结果,可以看出,读写锁运行的效率更高

退出移动版