1.面试题1:三个线程读,三个线程写同一个数据
public class ReadWriteLockTest { public static void main(String[] args) {
final Queue3 q3 = new Queue3();
for(int i=0;i<3;i++){
//3个读线程
new Thread(new Runnable() {
@Override
public void run() {
while(true){
q3.get();
}
}
}).start();
//3个写线程
new Thread(new Runnable() {
@Override
public void run() {
q3.set(new Random().nextInt(10000));
}
}).start();
}
}
} class Queue3{
private Object data = null; //共享数据,只能有一个线程能写该数据,但可以有多个线程同时读
ReadWriteLock lock = new ReentrantReadWriteLock();
public void get(){
try {
lock.readLock().lock();
System.out.println(Thread.currentThread().getName()+" be ready to read data!");
Thread.sleep((long)(Math.random()*1000));
System.out.println(Thread.currentThread().getName()+" have read data: "+data);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
lock.readLock().unlock();
}
}
public void set(Object data){
try {
lock.writeLock().lock();
System.out.println(Thread.currentThread().getName()+" be ready to write data!");
Thread.sleep((long)(Math.random()*1000));
this.data = data;
System.out.println(Thread.currentThread().getName()+" hava write data: "+data);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
lock.writeLock().unlock();
} } }
2.
Hibernate中两者的区别
① User user = session.load(id,User.class);
② User user = session.get(id,User.class);
其中②直接从数据库查询,如果查询为空,user为null
其中①在查询时,无论数据路有没有都会得到:User$Proxy 代理类,是一个缓存的User
如果实际的realUser为空则查询数据库,如果从数据库查询出的为空,抛异常,如果不为空
直接返回realUser.getName()
User$Proxy extends User{
private Integer id = id;
User realUser = null;
getName(){
if(realUser == null){
realUser = session.get(id);
if(realUser == null)
throw exception //抛异常
}
return realUser.getName();
}
}
3.javaAPI 上的一段代码:
Sample usages. Here is a code sketch showing how to exploit reentrancy to perform lock downgrading after updating a cache (exception handling is elided for simplicity): class CachedData {
Object data;
volatile boolean cacheValid;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); void processCachedData() { //处理数据
rwl.readLock().lock(); //多个线程并发读,不冲突,只需要上一个读锁
if (!cacheValid) { //检查缓存中有没有数据 if中的代码好比第一次获取数据
// Must release read lock before acquiring write lock
rwl.readLock().unlock(); //如果一个线程发现没有数据,释放读锁,获取写锁
rwl.writeLock().lock();
// Recheck state because another thread might have acquired
// write lock and changed state before we did.
if (!cacheValid) {
data = ... //实际的写数据逻辑
cacheValid = true; //缓存标志改为true,表示有缓存数据了
}
// Downgrade by acquiring read lock before releasing write lock
rwl.readLock().lock();
rwl.writeLock().unlock(); // Unlock write, still hold read
} use(data); //使用数据
rwl.readLock().unlock();
}
}
4.面试题:设计一个缓存系统
public class CacheDemo { private Map<String,Object> cache = new HashMap<>();
public static void main(String[] args) { } private ReadWriteLock rwl = new ReentrantReadWriteLock();
public Object getData(String key){
rwl.readLock().lock(); //获取读锁
Object value = null;
try {
value = cache.get(key);
if(value == null){
rwl.readLock().unlock();
rwl.writeLock().lock();
try {
if(value == null){
value = "aaa"; //实际是去数据库查询
}
} finally{
rwl.writeLock().unlock();
}
rwl.readLock().lock();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
rwl.readLock().unlock(); //释放读锁
}
return value;
} }