ReentrantReadWriteLock读写锁的示例

结论:Java中ReentrantReadWriteLock对写互斥,对读共享。


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {

    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    final Lock readLock = readWriteLock.readLock();
    final Lock writeLock = readWriteLock.writeLock();

    public static void main(String[] args) throws InterruptedException {
//        new ReadWriteLockTest().writeRead();
//        new ReadWriteLockTest().readRead();
//        new ReadWriteLockTest().readWrite();
//        new ReadWriteLockTest().writeWrite();
        new ReadWriteLockTest().writeReadWrite();
    }

    /**
     * 读 -> 写。同时有写锁和读锁时,写锁必须阻塞等待读锁读完,写锁才能进去。
     * @throws InterruptedException
     */
    private void readWrite() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求读锁", Thread.currentThread().getId()));
                    readLock.lock();
                    System.out.println(String.format("tid[%s]: w获得读锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放读锁", Thread.currentThread().getId()));
                    readLock.unlock();
                }
            }
        }).start();
        // 为了保证顺序,休眠一会再调度
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
    }

    /**
     * 写 -> 读 -> 写。
     * 后面写锁是否会优先调度?答:不会,还是会按顺序调度
     * @throws InterruptedException
     */
    private void writeReadWrite() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
        // 为了保证顺序,休眠一会再调度
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求读锁", Thread.currentThread().getId()));
                    readLock.lock();
                    System.out.println(String.format("tid[%s]: w获得读锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放读锁", Thread.currentThread().getId()));
                    readLock.unlock();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
    }

    /**
     * 写 -> 读。同时有写锁和读锁时,读锁必须等待写锁写完后,才能获得锁
     * @throws InterruptedException
     */
    private void writeRead() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
        // 为了保证顺序,休眠一会再调度
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求读锁", Thread.currentThread().getId()));
                    readLock.lock();
                    System.out.println(String.format("tid[%s]: w获得读锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放读锁", Thread.currentThread().getId()));
                    readLock.unlock();
                }
            }
        }).start();
    }

    /**
     * 读 -> 读,读锁可以共享
     */
    private void readRead() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求读锁", Thread.currentThread().getId()));
                    readLock.lock();
                    System.out.println(String.format("tid[%s]: w获得读锁", Thread.currentThread().getId()));
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放读锁", Thread.currentThread().getId()));
                    readLock.unlock();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求读锁", Thread.currentThread().getId()));
                    readLock.lock();
                    System.out.println(String.format("tid[%s]: w获得读锁", Thread.currentThread().getId()));
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放读锁", Thread.currentThread().getId()));
                    readLock.unlock();
                }
            }
        }).start();
    }

    /**
     * 写 -> 写。同时有写锁和写锁时,写锁必须等待上一个写锁写完后,才能获得锁
     * @throws InterruptedException
     */
    private void writeWrite() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
        // 为了保证顺序,休眠一会再调度
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(String.format("tid[%s]: w请求写锁", Thread.currentThread().getId()));
                    writeLock.lock();
                    System.out.println(String.format("tid[%s]: w获得写锁", Thread.currentThread().getId()));
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(String.format("tid[%s]: w释放写锁", Thread.currentThread().getId()));
                    writeLock.unlock();
                }
            }
        }).start();
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值