Below is a typical template for reading and writing (many messages and several records)
private ReadWriteLock lock = new ReentrantReadWriteLock();
private int value;
public void writeValue(int newValue){
lock.writeLock().lock();
try{
this.value = newValue;
}
finally{
lock.writeLock().unlock();
}
}
public int readValue(){
lock.readLock().lock();
try{
return value;
}
finally{
lock.writeLock().unlock();
}
}
I am wondering if it is possible to have priority for the writer and the reader? For example, usually a writer can wait a very long time (maybe forever) if there is a constant reading of locks held by another thread, so it is possible to have a writer with a higher priority, so whenever a writer arrives, it can be considered its high priority ( skip line), something like this.
peter source
share