ReentrantReadWriteLock类为读写锁。
读写锁也有两个锁,一个是读操作相关的锁,称为共享锁;
另一个是写操作相关的锁,也叫排他锁。
也就是多个读锁之间不互斥,读锁与写锁互斥,写锁与写锁互斥。
一个线程拥有了对象A的写锁,在释放写锁前其他线程无法获得A的读锁、写锁,因此其他线程此时无法读写;
一个线程拥有了对象A的读锁,在释放前其他线程可以获得A的读锁但无法获得A的写锁,因此其他线程此时可以读不可以写。
不加读锁的话其他线程是可以读,但也可以写,这时就可能导致数据不一致了
读锁共享
使用lock.readLock()读锁可以提高程序运行效率,允许多个线程同时执行lock()方法后面的代码
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Service {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void read(){
try {
lock.readLock().lock();
System.out.println("获得读锁"+Thread.currentThread().getName()+" "+System.currentTimeMillis());
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.readLock().unlock();
}
}
}
public class ThreadA extends Thread{
private Service service = new Service();
public ThreadA(Service service){
super();
this.service = service;
}
@Override
public void run(){
service.read();
}
}
public class ThreadB extends Thread{
private Service service = new Service();
public ThreadB(Service service){
super();
this.service = service;
}
@Override
public void run(){
service.read();
}
}
public class Run {
public static void main(String[] args) {
Service service = new Service();
ThreadA a = new ThreadA(service);
ThreadB b = new ThreadB(service);
a.setName("A");
b.setName("B");
a.start();
b.start();
}
}
写锁互斥
使用写锁代码lock.writeLock()的效果就是同一时间只允许一个线程执行lock()方法后面的代码
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Service {
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void writeLock(){
try {
lock.writeLock().lock();
System.out.println("进入写锁:"+Thread.currentThread().getName()+" 时间:"+System.currentTimeMillis());
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
lock.writeLock().unlock();
}
}
}
public class ThreadA extends Thread{
private Service service;
public ThreadA(Service service){
super();
this.service = service;
}
@Override
public void run(){
service.writeLock();
}
}
public class ThreadB extends Thread{
private Service service;
public ThreadB(Service service){
super();
this.service = service;
}
@Override
public void run(){
service.writeLock();
}
}
public class Run {
public static void main(String[] args){
Service s = new Service();
ThreadA a = new ThreadA(s);
ThreadB b = new ThreadB(s);
a.setName("A");
b.setName("B");
a.start();
b.start();
}
}