Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Example of the executor service
class UserRepository implements Closeable {
private FileStore fileStore;
private ConcurrentHashMap<UUID, User> cache = new ConcurrentHashMap<>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
private ExecutorService executors = Executors.newFixedThreadPool(2);
UserRepository(FileStore fnileStore) {
this.fileStore = fileStore;
}
@Override
public CompletableFuture<Void> saveUser(User user) {
return CompletableFuture.runAsync(() -> {
lock.writeLock().lock();
fileStore.write(user.getId(), user);
cache.put(user.getId(), user);
lock.writeLock().unlock();
}, executors);
}
@Override
public void close() {
executors.isShutdown()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment