I am implementing a distributed mutex, and I need to keep track of all the requests that have been made.
I've a message class which is comparable and I've a modified ArrayList
requestList = new ArrayList<Message>() {
public synchronized boolean add(Message msg) {
boolean ret = super.add(msg);
Collections.sort(requestList);
return ret;
}
I suspect that this requestList is getting modified by two threads and I'm seeing elements at the top of the list which should not be there. how do I make this requestList thread safe?
would doing as follows work?
requestList = Collections.synchronizedList(new ArrayList<Message>() {
public synchronized boolean add(Message msg) {
boolean ret = super.add(msg);
Collections.sort(requestList);
return ret;
});
also how does Collection.synchronizedList work? by sort of putting a 'synchronized' for all the methods of the ArrayList?
.javafiles for the various Java classes somewhere in your JDK installation and/or IDE and/or some online services somewhere.