|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.clapper.util.misc.ObjectLockSemaphore
public class ObjectLockSemaphore
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 Summary | |
|---|---|
ObjectLockSemaphore(int initialCount)
Deprecated. Allocate a new semaphore with the specified initial count. |
|
| Method Summary | |
|---|---|
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. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public ObjectLockSemaphore(int initialCount)
initialCount - Initial semaphore count.| Method Detail |
|---|
public boolean acquire(long timeout)
throws SemaphoreException
acquire in interface Semaphoretimeout - 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 SemaphoreSemaphoreException - error attempting to acquire semaphoreacquire(long)
public void addToCount(int delta)
throws SemaphoreException
addToCount in interface Semaphoredelta - The amount by which to increment the count.
SemaphoreException - error updating semaphore's count
public int getValue()
throws SemaphoreException
getValue in interface SemaphoreSemaphoreException - error getting semaphore's value
public void release()
throws SemaphoreException
release in interface SemaphoreSemaphoreException - error getting semaphore's valuepublic java.lang.String toString()
toString in class java.lang.Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||