public final class Mutex extends Object
releaseLock()
or
call releaseAllLocks()
before the mutex can be acquired by a
different thread.
The owner thread must explicitly release the lock before the thread is disposed of, otherwise it will leave a locked mutex behind.
Constructor and Description |
---|
Mutex() |
Modifier and Type | Method and Description |
---|---|
boolean |
acquireLock()
Waits indefintely to acquire the lock and it is equivalent to calling
acquireLock(0) . |
boolean |
acquireLock(long timeout)
Acquires a lock on this mutex.
|
int |
getLockCount()
Returns the number of locks the calling thread has on this mutex.
|
boolean |
isLockedByCurrentThread()
Returns
true if the current calling thread has at least one
lock on this mutex. |
void |
releaseAllLocks()
Releases all locks that the calling thread holds on this mutex.
|
int |
releaseLock()
Releases one lock on this mutex.
|
public final boolean acquireLock()
acquireLock(0)
.acquireLock(long)
public final boolean acquireLock(long timeout)
releaseLock()
or
by calling releaseAllLocks()
once.timeout
- The time in milliseconds to wait to obtain the lock. If
the timeout is 0, the thread will wait indefinitely to
obtain the lock.true
if the lock was successfully acquired or
false
if the lock could not be acquired.public final int releaseLock()
RuntimeException
is thrown. The counter will be decremented by one. If the counter reaches
zero, the mutex will be completely unlocked and other threads waiting on
this mutex will be able to proceed.
Returns how many locks the calling thread still has left on this mutex,
the same as calling getLockCount()
public final void releaseAllLocks()
public final boolean isLockedByCurrentThread()
true
if the current calling thread has at least one
lock on this mutex. If a different thread other than the lock
owner threaed tries to release the lock a RuntimeException
is thrown.public final int getLockCount()
Copyright © 2023. All rights reserved.