SemaphoreImplpublic class SemaphoreImpl extends Object implements SemaphoreImplements a simple semaphore. |
Fields Summary |
---|
private static final com.sun.jdo.spi.persistence.utility.logging.Logger | _loggerWhere to log messages about locking operations | private final String | _ownerFor logging, indicates on whose behalf locking is done. | private final Object | _lockSynchronizes the lock. | private Thread | _holderThread which holds the lock. | private int | _counterSemaphore counter. | private static final ResourceBundle | messagesI18N message handler |
Constructors Summary |
---|
public SemaphoreImpl(String owner)
_owner = owner;
|
Methods Summary |
---|
public void | acquire()Acquire a lock.
boolean debug = _logger.isLoggable(Logger.FINEST);
if (debug) {
Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
_logger.finest("utility.semaphoreimpl.acquire",items); // NOI18N
}
synchronized (_lock) {
//
// If the current thread already holds this lock, we simply
// update the count and return.
//
if (Thread.currentThread() == _holder) {
_counter++;
} else {
while (_counter > 0) {
try {
// wait for the lock to be released
_lock.wait();
} catch (InterruptedException e) {
}
}
_holder = Thread.currentThread();
_counter++;
if (debug) {
Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
_logger.finest("utility.semaphoreimpl.gotlock",items); // NOI18N
}
}
}
| public void | release()Release a lock.
boolean debug = _logger.isLoggable(Logger.FINEST);
if (debug) {
Object[] items = new Object[] {_owner, Thread.currentThread(),new Integer(_counter)};
_logger.finest("utility.semaphoreimpl.release",items); // NOI18N
}
synchronized (_lock) {
//
// If the current thread already holds this lock, we simply
// update the count and return.
//
if (Thread.currentThread() == _holder) {
if (--_counter == 0) {
_holder = null;
_lock.notify();
}
} else {
throw new IllegalMonitorStateException(
I18NHelper.getMessage(messages,
"utility.semaphoreimpl.wrongthread", // NOI18N
new Object[] {_owner, Thread.currentThread()}));
}
}
|
|