public class AutoCloseableLock extends Object implements AutoCloseable
Constructor and Description |
---|
AutoCloseableLock()
Creates an instance of
AutoCloseableLock , initializes
the underlying lock instance with a new ReentrantLock . |
AutoCloseableLock(Lock lock)
Wrap provided Lock instance.
|
Modifier and Type | Method and Description |
---|---|
AutoCloseableLock |
acquire()
A wrapper method that makes a call to
lock() of the underlying
ReentrantLock object. |
void |
close()
Attempts to release the lock by making a call to
release() . |
Condition |
newCondition()
|
void |
release()
A wrapper method that makes a call to
unlock() of the
underlying ReentrantLock object. |
boolean |
tryLock()
A wrapper method that makes a call to
tryLock() of
the underlying Lock object. |
public AutoCloseableLock()
AutoCloseableLock
, initializes
the underlying lock instance with a new ReentrantLock
.public AutoCloseableLock(Lock lock)
lock
- Lock instance to wrap in AutoCloseable API.public AutoCloseableLock acquire()
lock()
of the underlying
ReentrantLock
object.
Acquire teh lock it is not held by another thread, then sets
lock held count to one, then returns immediately.
If the current thread already holds the lock, increase the lock
help count by one and returns immediately.
If the lock is held by another thread, the current thread is
suspended until the lock has been acquired by current thread.ReentrantLock
object itself. This is to
support try-with-resource syntax.public void release()
unlock()
of the
underlying ReentrantLock
object.
Attempts to release the lock.
If the current thread holds the lock, decrements the hold
count. If the hold count reaches zero, the lock is released.
If the current thread does not hold the lock, then
IllegalMonitorStateException
is thrown.public void close()
release()
.
This is to implement close()
method from AutoCloseable
interface. This allows users to user a try-with-resource syntax, where
the lock can be automatically released.close
in interface AutoCloseable
public boolean tryLock()
tryLock()
of
the underlying Lock
object.
If the lock is not held by another thread, acquires the lock, set the
hold count to one and returns true
.
If the current thread already holds the lock, the increment the hold
count by one and returns true
.
If the lock is held by another thread then the method returns
immediately with false
.true
if the lock was free and was acquired by the
current thread, or the lock was already held by the current
thread; and false
otherwise.public Condition newCondition()
Copyright © 2017 Apache Software Foundation. All Rights Reserved.