|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.objectweb.jac.util.Semaphore
This is a simple implementation of the well-known semaphore synchronization feature.
It allows a given number of thread to be blocked waiting for a resource to be freed by the threads that are currently using it.
Field Summary | |
protected int |
count
The available resources count. |
Constructor Summary | |
Semaphore()
The default constructor for this semaphore equivalent to Semaphore(0) .
|
|
Semaphore(int n)
The semaphore constructor that allows the programmer to assign a number of resources item that are greater than 0 or 1 (meaning that several threads (n exactly) can use the semaphore at the same time). |
Method Summary | |
boolean |
acquire()
Acquires a resource on this semaphore with no timeout. |
boolean |
acquire(long timeout)
Acquires a resource on this semaphore. |
int |
getCount()
|
int |
getWaitingCount()
Gets the number of threads that are currently blocked on this semaphore. |
void |
release()
Releases a resource on this semaphore. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected int count
Constructor Detail |
public Semaphore(int n)
n
- the number of resource itemspublic Semaphore()
Semaphore(0)
.
This constructor means that no resources are available by default. Thus, the first thread (t1) that will acquire the semaphore will eventually by blocked until another thread (t2) releases a resource on this semaphore.
This use of the semaphore feature is quite strange regarding resources (since the deblocking thread (t2) releases a resource that it never acquired!!) but it can be very usefull for synchronization especially if t1 waits for a result that is being calculating by t2 and that is not yet available.
Method Detail |
public int getWaitingCount()
This method is an indication but is not safe since a thread can be released meantime you evaluate its result.
public int getCount()
public boolean acquire()
If no resources are available anymore (count == 0), then the thread that performed this call is blocked until another thread releases a resource.
acquire(long)
,
release()
public boolean acquire(long timeout)
If no resources are available anymore (count == 0), then the thread that performed this call is blocked until another thread releases a resource.
timeout
- milliseconds to wait for. 0 means wait for ever.
acquire()
,
release()
public void release()
If one or several threads are blocked, waiting for a resource to be available, then this call will necessaraly makes one of them take the semaphore's monitor (the others, if any will block again). There is absolutly no way of knowing which thread will take the monitor first.
acquire()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |