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();
}
}
}