Skip to content

Instantly share code, notes, and snippets.

@cbeer
Created January 10, 2010 19:43
Show Gist options
  • Save cbeer/273717 to your computer and use it in GitHub Desktop.
Save cbeer/273717 to your computer and use it in GitHub Desktop.
private final Set<String> m_lockedPIDs;
[...]
/**
* Creates a new DefaultDOManager.
*/
public DefaultDOManager(Map<String, String> moduleParameters, Server server, String role)
throws ModuleInitializationException {
super(moduleParameters, server, role);
m_lockedPIDs = new HashSet<String>();
}
[...]
private void releaseWriteLock(String pid) {
synchronized (m_lockedPIDs) {
m_lockedPIDs.remove(pid);
}
}
private void getWriteLock(String pid) throws ObjectLockedException {
synchronized (m_lockedPIDs) {
if (m_lockedPIDs.contains(pid)) {
throw new ObjectLockedException(pid + " is currently being "
+ "modified by another thread");
} else {
m_lockedPIDs.add(pid);
}
}
}
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
[...]
private final HashMap<String, Lock> m_lockedPIDs;
[...]
/**
* Creates a new DefaultDOManager.
*/
public DefaultDOManager(Map<String, String> moduleParameters, Server server, String role)
throws ModuleInitializationException {
super(moduleParameters, server, role);
m_lockedPIDs = new HashMap<String, Lock>();
}
[...]
private void releaseWriteLock(String pid) {
synchronized (m_lockedPIDs) {
m_lockedPIDs.remove(pid).unlock();
}
}
private void getWriteLock(String pid) throws ObjectLockedException {
synchronized (m_lockedPIDs) {
if ( m_lockedPIDs.containsKey(pid)) {
Lock w1 = m_lockedPIDs.get(pid);
try {
if( !w1.tryLock(5000, TimeUnit.SECONDS)) {
throw new ObjectLockedException(pid + " is currently being "
+ "modified by another thread");
}
} catch (InterruptedException e) {
throw new ObjectLockedException(pid + " is currently being "
+ "modified by another thread");
}
}
ReentrantReadWriteLock lck = new ReentrantReadWriteLock();
Lock w = lck.writeLock();
w.lock();
m_lockedPIDs.put(pid, w);
}
}
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment