I am trying to prevent some concurrently running
threads from trampling over each other in running some
processes from a servlet. In order to do this, I have
[tried to] implemented a simple concurrency manager.
It is a Singleton and contains a hashtable that stores
lock names. Before a thread runs a particular method,
it tries to set a lock name. If it is already set, the
set fails.
Simple enough... but I cannot get it to work. It
starts out fine, but eventually, it seems like my
threads are getting different instances (of my
SINGLETON !!!) because a periodic dump of the
hashtable shows different values.
Here is the code:
import java.util.*;
public class JConcurrencyManager implements
ConcurrencyManager {
private Hashtable _mutex = null;
private static JConcurrencyManager _cm = new
JConcurrencyManager();
private JConcurrencyManager() {
if(_mutex==null) _mutex = new Hashtable(100);
}
public static JConcurrencyManager getInstance() {
return _cm;
}
public synchronized boolean setAttribute(String
name) {
if(_mutex.containsKey(name)) return false;
_mutex.put(name, new Object());
return true;
}
public synchronized void removeAttribute(String
name) {
_mutex.remove(name);
}
public synchronized boolean isAttributeStored(String
name) {
if(_mutex.containsKey(name)) return false;
else return true;
}
public synchronized boolean
areAttributesStored(String[] names) {
for(int i=0; i < names.length; i++) {
if(!isAttributeStored(names[i])) return false;
}
return true;
}
public synchronized void removeAttributes(String[]
names) {
for(int i=0; i < names.length; i++) {
removeAttribute(names[i]);
}
}
public synchronized Enumeration getAttributes() {
return _mutex.keys();
}
}
Probably a stupid error somewhere in this code,
because it should be idiot proof, ie. private
constructor, getInstance etc.
Any help appreciated......
//Nicholas
=====
"And what causes me to run?
'Cause you're a...
Big Black Furry Creature From Mars"
--
Nicholas Whitehead
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://messenger.yahoo.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html