I'm thinking of a solution for a problem, I want to synchronized a List, which will be accessed by two Type of Thread:
- A: Thread wchill add/remove on the list.
- B: Thread will check if the list contains a specific object.
The solution that i thinked about is :
SingletonManager will be synchronized:
public class SingletonManager {
// Instance of singleton Synchronized
private static Map<String, String> myList = new ConcurrentHashMap<>();
private static synchronized void addElement(String key, String value) {
myList.put(key,value);
}
private static synchronized String getElement(String key) {
return myList.get(key);
}
}
The problem, is that the Thread A cannot try to add/remove on the list while Thread B is checking if an element is contained on the List. (because of singleton design of the Manager)
And my need is that both thread B and A access simultaneously to the list for differents task. And synchronization should stay only on method delegate for each Thread.
Which solution can you advice me?
I also thinked of a solution to make two instances of the Manager by each type of thread. What do you think about it ?
Many thanks :)