读写锁小结(ReentrantReadWriteLock)
读-读不互斥:让读写锁比普通锁有更高的并发性。是lock能取代synchronized的原因之一。
模拟多线程对 缓存资源类map 的读、写操作。要保证写操作的原子性 和 独占性 才算成功,即从开始写入 至 写入完成是一个不可分割的原子操作。
读写锁验证代码
1.未加锁前代码
package lock;
import java.util.HashMap;
import java.util.Map;
class MyResource {
private volatile Map<String, Object> map = new HashMap<>();
public void put(String key, Object value) {
System.out.println("线程" + Thread.currentThread().getName() + " : 开始写入(未完成)");
try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }
map.put(key, value);
System.out.println("线程" + Thread.currentThread().getName() + " : 写入完成 " + value);
}
public void get(String key) {
System.out.println("线程" + Thread.currentThread().getName() + " : 开始读取");
try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("线程" + Thread.currentThread().getName() + " : 读取到 " + map.get(key));
}
}
public class ReadWriteLock {
public static void main(String[] args) {
MyResource myResource = new MyResource();
for(int i = 0; i < 5; i++) {
final int tempInt = i;
new Thread(() -> {
myResource.put(tempInt + "", tempInt + "");
}, String.valueOf(i)).start();
}
for(int i = 0; i < 5; i++) {
final int tempInt = i;
new Thread(() -> {
myResource.get(tempInt + "");
}, String.valueOf(i)).start();
}
}
}
如下运行截图未达到写操作的原子性 和 独占性
2.加锁后代码
package lock;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
class MyResource {
private volatile Map<String, Object> map = new HashMap<>();
private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public void put(String key, Object value) {
lock.writeLock().lock();
try {
System.out.println("线程" + Thread.currentThread().getName() + " : 开始写入(未完成)");
try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }
map.put(key, value);
System.out.println("线程" + Thread.currentThread().getName() + " : 写入完成 " + value);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}
}
public void get(String key) {
lock.readLock().lock();
try {
System.out.println("线程" + Thread.currentThread().getName() + " : 开始读取");
try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }
System.out.println("线程" + Thread.currentThread().getName() + " : 读取到 " + map.get(key));
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
}
}
public class ReadWriteLock {
public static void main(String[] args) {
MyResource myResource = new MyResource();
for(int i = 0; i < 5; i++) {
final int tempInt = i;
new Thread(() -> {
myResource.put(tempInt + "", tempInt + "");
}, String.valueOf(i)).start();
}
for(int i = 0; i < 5; i++) {
final int tempInt = i;
new Thread(() -> {
myResource.get(tempInt + "");
}, String.valueOf(i)).start();
}
}
}
运行截图可知每个写操作都是原子性的