Created
January 10, 2010 19:43
-
-
Save cbeer/273717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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