public class ObjectLockSemaphore extends java.lang.Object implements Semaphore
NOTE: This class, and its parent interface, are deprecated. With J2SE 5.0 (a.k.a., Java 1.5), the JDK provides its own semaphore class, java.util.concurrent.Semaphore.
The ObjectLockSemaphore class implements the Semaphore
interface and provides a classic counting semaphore that uses the Java
object-locking primitives (the same locking primitives used to implement
synchronized critical sections) to ensure that
ObjectLockSemaphore objects are atomically created, accessed,
updated and destroyed.
Warning: Do not attempt to acquire or release a semaphore within a critical region--that is, within a "synchronized" section--or you'll risk deadlock. The Semaphore class is implemented using the Java VM's object monitor capability, the same capability that controls how synchronized sections work. The following code fragment is likely to cause a deadlock:
private ArrayList bufferPool = ...; private Semaphore sem = new ObjectLockSemaphore (bufferPool.size()); public MyBuffer getBuffer() { MyBuffer result = null; synchronized (bufferPool) { // bufferPool is now locked sem.acquire(); // bufferPool is still locked result = (MyBuffer) bufferPool.removeElementAt (0); } return result; } public void returnBuffer (MyBuffer buf) { synchronized (bufferPool) { // bufferPool is now locked bufferPool.addElement (buf); } sem.release(); // bufferPool is still locked }
Given the above code, assume:
Here's how the deadlock can occur:
This particular deadlock situation is easily avoided as shown:
private ArrayList bufferPool = ...; private Semaphore sem = new ObjectLockSemaphore (bufferPool.size()); public MyBuffer getBuffer() { MyBuffer result = null; sem.acquire(); synchronized (bufferPool) { result = (MyBuffer) bufferPool.removeElementAt (0); } return result; } public void returnBuffer (MyBuffer buf) { synchronized (bufferPool) { // bufferPool is now locked bufferPool.addElement (buf); } sem.release(); // bufferPool is still locked }
Constructor and Description |
---|
ObjectLockSemaphore(int initialCount)
Deprecated.
Allocate a new semaphore with the specified initial count.
|
Modifier and Type | Method and Description |
---|---|
boolean |
acquire()
Deprecated.
Acquire this semaphore.
|
boolean |
acquire(long timeout)
Deprecated.
Acquire this semaphore.
|
void |
addToCount(int delta)
Deprecated.
Increment the semaphore's current value, as well as its maximum value.
|
int |
getValue()
Deprecated.
Get the semaphore's current value (i.e., its count).
|
void |
release()
Deprecated.
Release this semaphore, incrementing its counter.
|
java.lang.String |
toString()
Deprecated.
Return a string representation of the semaphore.
|
public ObjectLockSemaphore(int initialCount)
initialCount
- Initial semaphore count.public boolean acquire(long timeout) throws SemaphoreException
acquire
in interface Semaphore
timeout
- Timeout period, in milliseconds. A value of 0 means
"wait forever, until the semaphore is available." A
negative value means "return immediately if the
semaphore is not available."SemaphoreException
- error attempting to acquire semaphoreacquire()
public boolean acquire() throws SemaphoreException
acquire(long)
with a timeout value of 0.acquire
in interface Semaphore
SemaphoreException
- error attempting to acquire semaphoreacquire(long)
public void addToCount(int delta) throws SemaphoreException
addToCount
in interface Semaphore
delta
- The amount by which to increment the count.SemaphoreException
- error updating semaphore's countpublic int getValue() throws SemaphoreException
getValue
in interface Semaphore
SemaphoreException
- error getting semaphore's valuepublic void release() throws SemaphoreException
release
in interface Semaphore
SemaphoreException
- error getting semaphore's valuepublic java.lang.String toString()
toString
in class java.lang.Object