Created
October 16, 2020 06:44
-
-
Save anton0xf/967c41d71dbe925467b68d199e77963e to your computer and use it in GitHub Desktop.
OK task 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface OffMap { | |
long get(); | |
void close(); | |
} | |
class Wrap implements OffMap { | |
private final OffMap map; | |
private volatile boolean closed = false; | |
private final ReadWriteLock lock = new ReentrantReadWriteLock(); | |
public Wrap(OffMap map) { | |
this.map = map; | |
} | |
@Override | |
public long get() { | |
Lock readLock = this.lock.readLock(); | |
readLock.lock(); | |
try { | |
if (closed) { | |
throw new UnsupportedOperationException("Already closed"); | |
} | |
return map.get(); | |
} finally { | |
readLock.unlock(); | |
} | |
} | |
@Override | |
public void close() { | |
Lock writeLock = this.lock.writeLock(); | |
writeLock.lock(); | |
try { | |
if (closed) { | |
return; | |
} | |
closed = true; | |
map.close(); | |
} finally { | |
writeLock.unlock(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OffMap это какой-то тяжёлый ресурс. Надо его аккуратно синхронизировать