In our system, we have a method that will do some work when it\'s called with a certain ID:
public void doWork(long id) { /* ... */ }
Now, this
I'd say you're already pretty far to having a solution. Make a LockManager
who lazily and reference-counted-ly manages those locks for you. Then use it in doWork
:
public void doWork(long id) {
LockObject lock = lockManager.GetMonitor(id);
try {
synchronized(lock) {
// ...
}
} finally {
lock.Release();
}
}