java并发初探ReentrantWriteReadLock

ReenWriteReadLock类的优秀博客

ReentrantReadWriteLock读写锁详解

Java多线程系列--“JUC锁”08之 共享锁和ReentrantReadWriteLock

ReentrantWriteReadLock类实现

java并发初探ReentrantWriteReadLock-LMLPHP

@startuml
interface Lock
interface ReadWriteLock
class ReentrantReadWriteLock{
Sync sync
ReentrantReadWriteLock.ReadLock readerLock;
ReentrantReadWriteLock.WriteLock writerLock;
}
ReadWriteLock <|-- ReentrantReadWriteLock
ReentrantReadWriteLock --o ReadLock
ReentrantReadWriteLock --o WriteLock
ReentrantReadWriteLock --o Sync
AbstractQueuedSynchronizer <|- Sync
Lock <|-- ReadLock
Lock <|-- WriteLock
@enduml

特点

ReentrantWriteReadLock内部有读锁和写锁。

写锁是独占锁

当前线程持有写锁,其他线程只能等待

当其他线程拥有对象的写锁或者读锁,当前线程等待获取写锁

读锁是共享锁

没有其他线程的写锁,当前线程可以获取读锁

使用例子

package com.java.javabase.thread.base.concurrent.lock;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; @Slf4j
public class ReentranReadWriteLockTest {
/**
* @author
* @version
* @since
*/
public static void main(String[] args) {
ReentranReadWriteLockTest test =new ReentranReadWriteLockTest();
User user =test.new User("jack",test.new Count("001",1000));
for(int i=0;i<3;i++)
{
user.printCash();
user.setCash(1000);
}
} @Data
@AllArgsConstructor
@NoArgsConstructor
class Count {
private String id;
private int cash;
} @Data
class User {
private String name;
private Count count;
private ReadWriteLock readWriteLock; public User(String name, Count count) {
this.name = name;
this.count = count;
this.readWriteLock = new ReentrantReadWriteLock();
} public void setCash(final int cash) {
new Thread() {
@Override
public void run() {
readWriteLock.writeLock().lock();
try {
log.info("thread {} :user {} set cash {} start",
Thread.currentThread().getName(), name, cash);
Thread.sleep(1000);
log.info("thread {} :user {} current cash = {}",
Thread.currentThread().getName(), name, count.getCash());
count.setCash(cash);
log.info("thread {} :user {} set cash {} end",
Thread.currentThread().getName(), name, cash);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
readWriteLock.writeLock().unlock();
}
} }.start(); } public void printCash() {
new Thread() {
@Override
public void run() {
try {
readWriteLock.readLock().lock();
log.info("thread {} :user {} get cash start", Thread.currentThread().getName(), name);
log.info("thread {} :user {} current cash = {}",
Thread.currentThread().getName(), name, count.getCash());
Thread.sleep(1000);
log.info("thread {} :user {} get cash end", Thread.currentThread().getName(), name);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readWriteLock.readLock().unlock();
} }
}.start();
}
}
}

例子结果

2019-08-09 10:49:15,618   [Thread-0] INFO  ReentranReadWriteLockTest  - thread Thread-0 :user jack get  cash start
2019-08-09 10:49:15,618 [Thread-0] INFO ReentranReadWriteLockTest - thread Thread-0 :user jack current cash = 1000
2019-08-09 10:49:16,618 [Thread-0] INFO ReentranReadWriteLockTest - thread Thread-0 :user jack get cash end
2019-08-09 10:49:16,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack set cash 1000 start
2019-08-09 10:49:17,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack current cash = 1000
2019-08-09 10:49:17,618 [Thread-1] INFO ReentranReadWriteLockTest - thread Thread-1 :user jack set cash 1000 end
2019-08-09 10:49:17,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack get cash start
2019-08-09 10:49:17,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack get cash start
2019-08-09 10:49:17,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack current cash = 1000
2019-08-09 10:49:17,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack current cash = 1000
2019-08-09 10:49:18,618 [Thread-4] INFO ReentranReadWriteLockTest - thread Thread-4 :user jack get cash end
2019-08-09 10:49:18,618 [Thread-2] INFO ReentranReadWriteLockTest - thread Thread-2 :user jack get cash end
2019-08-09 10:49:18,618 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack set cash 1000 start
2019-08-09 10:49:19,617 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack current cash = 1000
2019-08-09 10:49:19,617 [Thread-5] INFO ReentranReadWriteLockTest - thread Thread-5 :user jack set cash 1000 end
2019-08-09 10:49:19,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack set cash 1000 start
2019-08-09 10:49:20,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack current cash = 1000
2019-08-09 10:49:20,617 [Thread-3] INFO ReentranReadWriteLockTest - thread Thread-3 :user jack set cash 1000 end
05-11 20:07