A server project, might run for very long time and create many threads.
In the following code I ask myself do i have to protect the lock somehow in addition to an overall try catch in method setData(MyData data):
note: assuming its thread-safe, i am not really familiar with other reasons or natural disasters that may cause thread termination, like windows 7 limitations or so. (i am not greedy, using +-5 threads, but still)
public class MyFactory {
private ReadWriteLock rwl = new ReentrantReadWriteLock();
private Lock readLock = rwl.readLock();
private Lock writeLock = rwl.writeLock();
private static MyFactory _instance = new MyFactory();
private static Map<Integer, MyData> mapDatas = new HashMap<>();
public static MyFactory getInstance() {
return _instance;
}
public void setData(MyData data) {
writeLock.lock();
try {
mapData.put(...);
} catch (Exception exc) {
...
} finally {
writeLock.unlock();
}
}