My FileManager
exposes a write
method which contains a synchronized block to prevent concurrency issues:
class FileManager{
Object lock = new Object();
public void write() {
synchronized (lock) {
String id = nextId();
write(id);
}
}
}
However, if multiple instances of FileManager
exist, their write methods can still be executed concurrently. Would making FileManager a singleton be a good fix to this possible concurrency issue? Or should I use syncrhonized
keyword in another way?