ReadWriteLock
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCacheLock myCache = new MyCacheLock();
for (int i = 1; i <= 5; i++) {
final int temp = i;
new Thread(()->{
myCache.put(temp+"",temp+"");
},String.valueOf(i)).start();
}
for (int i = 1; i <= 5; i++) {
final int temp = i;
new Thread(()->{
myCache.get(temp+"");
},String.valueOf(i)).start();
}
}
}
class MyCacheLock {
private volatile Map<String,Object> map = new HashMap<>();
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public void put(String key,Object value) {
readWriteLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"寫入"+key);
map.put(key,value);
System.out.println(Thread.currentThread().getName()+"寫入OK");
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.writeLock().unlock();
}
}
public void get(String key) {
readWriteLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"讀取"+key);
Object o = map.get(key);
System.out.println(Thread.currentThread().getName()+"讀取OK");
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.readLock().unlock();
}
}
}
class MyCache {
private volatile Map<String,Object> map = new HashMap<>();
public void put(String key,Object value) {
System.out.println(Thread.currentThread().getName()+"寫入"+key);
map.put(key,value);
System.out.println(Thread.currentThread().getName()+"寫入OK");
}
public void get(String key) {
System.out.println(Thread.currentThread().getName()+"讀取"+key);
Object o = map.get(key);
System.out.println(Thread.currentThread().getName()+"讀取OK");
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結