ReentrantReadWriteLock用法

读读锁不互斥

public class ReentrantReadWriteLockDemo {

    public static  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public static ReentrantReadWriteLock.ReadLock readLock = lock.readLock();

    static class ThreadDemo implements Runnable{

        private String name;

        public ThreadDemo(String name){
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(name + "开始" + getDate());
            readLock.lock();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            readLock.unlock();
            System.out.println(name + "结束" + getDate());
        }
    }

    public static String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        return  dateFormat.format(date);
    }
    public static void main(String []args){
        ThreadPoolExecutor threadPoolExecutor =  new ThreadPoolExecutor(3, 3,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        for (int i=0;i<3;i++){
            threadPoolExecutor.execute(new ThreadDemo("task-"+i));
        }
        threadPoolExecutor.shutdown();
    }

}

结果如下,读读锁不互斥,三个线程同时结束

task-1开始2019-11-17 :03:31:45
task-2开始2019-11-17 :03:31:45
task-0开始2019-11-17 :03:31:45
task-1结束2019-11-17 :03:31:50
task-2结束2019-11-17 :03:31:50
task-0结束2019-11-17 :03:31:50

写写锁互斥

public class ReentrantReadWriteLockDemo2 {

    public static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public static ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

    static class ThreadDemo implements Runnable{

        private String name;

        public ThreadDemo(String name){
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(name + "开始" + getDate());
            writeLock.lock();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            writeLock.unlock();
            System.out.println(name + "结束" + getDate());
        }
    }

    public static String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        return  dateFormat.format(date);
    }
    public static void main(String []args){
        ThreadPoolExecutor threadPoolExecutor =  new ThreadPoolExecutor(3, 3,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        for (int i=0;i<3;i++){
            threadPoolExecutor.execute(new ThreadDemo("task-"+i));
        }
        threadPoolExecutor.shutdown();
    }

}

可以看到三个线程依次完成

task-2开始2019-11-17 :03:35:24
task-1开始2019-11-17 :03:35:24
task-0开始2019-11-17 :03:35:24
task-2结束2019-11-17 :03:35:29
task-1结束2019-11-17 :03:35:34
task-0结束2019-11-17 :03:35:39

读写互斥

public class ReentrantReadWriteLockDemo3 {

    public static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    public static ReentrantReadWriteLock.ReadLock readLock = lock.readLock();

    public static ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();

    static class ReadThreadDemo implements Runnable{

        private String name;

        public ReadThreadDemo(String name){
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(name + "开始" + getDate());
            readLock.lock();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            readLock.unlock();
            System.out.println(name + "结束" + getDate());
        }
    }

    static class WriteThreadDemo implements Runnable{

        private String name;

        public WriteThreadDemo(String name){
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(name + "开始" + getDate());
            writeLock.lock();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            writeLock.unlock();
            System.out.println(name + "结束" + getDate());
        }
    }

    public static String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        return  dateFormat.format(date);
    }
    public static void main(String []args){
        ThreadPoolExecutor threadPoolExecutor =  new ThreadPoolExecutor(3, 3,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue());
        threadPoolExecutor.execute(new ReadThreadDemo("task-0"));
        threadPoolExecutor.execute(new WriteThreadDemo("task-1"));
        threadPoolExecutor.shutdown();
    }
}

可以看到和写写锁互斥一样,读写也互斥

task-1开始2019-11-17 :03:43:05
task-0开始2019-11-17 :03:43:05
task-1结束2019-11-17 :03:43:10
task-0结束2019-11-17 :03:43:15

你可能感兴趣的:(ReentrantReadWriteLock用法)