Skip to content

Instantly share code, notes, and snippets.

@ThakurPriyanka
Created June 2, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThakurPriyanka/5b952e1adb3080b65df5bcd5d32a3ef8 to your computer and use it in GitHub Desktop.
Save ThakurPriyanka/5b952e1adb3080b65df5bcd5d32a3ef8 to your computer and use it in GitHub Desktop.
// 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