Created
June 2, 2020 07:23
-
-
Save ThakurPriyanka/5b952e1adb3080b65df5bcd5d32a3ef8 to your computer and use it in GitHub Desktop.
This file contains 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
// 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