测试ReentrantReadWriteLock的相应特性

ReentrantReadWriteLock
所在的包是java.util.concurrent.locks.ReentrantReadWriteLock,读写锁所包含的锁有俩中,一种是与读操作相关的锁也被称为共享锁,一种是与写操作相关的锁也被称为排它锁,在没有写入操作时,读锁之间互不排斥,多个线程都可以获取读锁,当在涉及写入操作时,只有一个线程可以获取写锁进行写入操作,也就是说,当在读取时,多个线程可以同时获取读锁,当在写入式,只有一个线程可以写入。
互斥情况:读写互斥,写写互斥,写读互斥。
读锁:ReentrantReadWriteLock.readLock().lock(),
写锁:ReentrantReadWriteLock.writeLock().lock(),
读读共享的示例:

package com.example.demo.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
测试ReentrantReadWriteLock的读读共享
 */
public class LockTest {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                5,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        LOCKService lockService = new LOCKService();
        for(int i=0;i<3;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        lockService.read();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
class LOCKService{
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void read(){
        try {
            try {
                //设置写锁
                lock.readLock().lock();
                System.out.println("获得读锁"+"线程名为:"+Thread.currentThread().getName()+"获取读锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                //释放读锁
                System.out.println("释放读锁:"+"线程名为:"+Thread.currentThread().getName()+"获取读锁时间为:"+System.currentTimeMillis());
                lock.readLock().unlock();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果:在这里插入图片描述写写互斥示例:

package com.example.demo.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
测试ReentrantReadWriteLock的写写互斥
 */
public class LockTest {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                5,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        LOCKService lockService = new LOCKService();
        for(int i=0;i<3;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        lockService.write();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
class LOCKService{
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void write(){
        try {
            try {
                //设置写锁
                lock.writeLock().lock();
                System.out.println("获得写锁"+"线程名为:"+Thread.currentThread().getName()+"获取写锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                //释放读锁
                System.out.println("释放写锁:"+"线程名为:"+Thread.currentThread().getName()+"获取写锁时间为:"+System.currentTimeMillis());
                lock.writeLock().unlock();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果:
在这里插入图片描述读写互斥

package com.example.demo.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
测试ReentrantReadWriteLock的读写互斥
 */
public class LockTest {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                5,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        LOCKService lockService = new LOCKService();
        for(int i=0;i<3;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        lockService.read();
                        lockService.write();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
class LOCKService{
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
    public void read(){
        try {
            try {
                //设置读锁
                lock.readLock().lock();
                System.out.println("获得读锁"+"线程名为:"+Thread.currentThread().getName()+"获取读锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                lock.readLock().unlock();
                System.out.println("释放读锁"+"线程名为:"+Thread.currentThread().getName()+"释放读锁时间为:"+System.currentTimeMillis());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void write(){
        try {
            try {
                //设置写锁
                lock.writeLock().lock();
                System.out.println("获得写锁"+"线程名为:"+Thread.currentThread().getName()+"获取写锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                //释放写锁
                System.out.println("释放写锁:"+"线程名为:"+Thread.currentThread().getName()+"釋放写锁时间为:"+System.currentTimeMillis());
                lock.writeLock().unlock();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果
在这里插入图片描述写读互斥:

package com.example.demo.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
测试ReentrantReadWriteLock的写读互斥
 */
public class LockTest {
    public static void main(String[] args) {
        ExecutorService executorService = new ThreadPoolExecutor(
                5,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5));
        LOCKService lockService = new LOCKService();
        for(int i=0;i<3;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        lockService.write();
                        lockService.read();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
class LOCKService{
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
    public void read(){
        try {
            try {
                //设置读锁
                lock.readLock().lock();
                System.out.println("获得读锁"+"线程名为:"+Thread.currentThread().getName()+"获取读锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                lock.readLock().unlock();
                System.out.println("释放读锁"+"线程名为:"+Thread.currentThread().getName()+"释放读锁时间为:"+System.currentTimeMillis());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void write(){
        try {
            try {
                //设置写锁
                lock.writeLock().lock();
                System.out.println("获得写锁"+"线程名为:"+Thread.currentThread().getName()+"获取写锁时间为:"+System.currentTimeMillis());
                Thread.sleep(10000);
            }finally {
                //释放写锁
                System.out.println("释放写锁:"+"线程名为:"+Thread.currentThread().getName()+"釋放写锁时间为:"+System.currentTimeMillis());
                lock.writeLock().unlock();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值