LOCK interface
Take a look at the structure of the LOCK interface and the specific implementation of the Lock interface. Reentrantlock, how much you know how much "lock" work is made by trying to figure it out.
LOCK interface:
Package edu.emory.mathcs.Backport.java.util.concurrent.locks;
Import edu.emory.mathcs.Backport.java.util.concurrent.timeUnit;
Public interface lock {
/ **
* Acquires the lock.
*
if the lock is not available kil
* The Current Thread Becomes Disabled for Thread Scheduling
* Purposes and lies dormant Until The Lock Has Been acquired.
*
Implementation considances b>
*
a lock tt> Implementation May Be Able To Detect
* Erroneous Use of the Lock, Such As An Invocation That Would Cause
* DEADLOCK, And May Throw an (unchecked) Exception in Such Circumstances.
* The circumstances and the exception type must be docutented by That
* Lock TT> Implementation.
*
** /
Void Lock ();
Method function:
Application lock
Detailed Description:
If the current lock is invalid (not available), the thread that is applied for this lock will hang the waiting until a valid lock.
Considering the implementation of the class:
Specific implementations are capable of detecting errors in use (such as deadlocks) and throw exceptions in accordance with the appropriate case. These specific abnormalities must be reflected in the implementation class of the LOCK.
/ **
* Acquires the lock unless the current thread is
* {@Link thread # interrupt interrupted}.
*
acquires the lock if it is available and returns immediately.
*
if the lock is not available kil
* The Current Thread Becomes Disabled for Thread Scheduling
* Purposes and lies dormant UnTil One of Two Things Happens:
*
*
*
* Thread, And Interruption of Lock Acquisition is supported.
* ul>
*
if the current thread:
*
*
* The Lock, And Interruption of Lock Acquisition Is Supported,
* ul>
* Ten {@Link InterruptedException} Is Thrown and the Current Thread's
* Interrupted status is cleared.
*
*
Implementation considances b>
*
*
The Ability to Interrupt a Lock Acquisition in Some
* Implementations May Not Be Possible, and if Possible May Be an
* Expensive Operation. The Programmer SHOULD BE Aware That this
* May be the case. An importation shop document when this is IS
* The case.
*
*
an importation can favor Responding to an an interrupt over
* Normal Method Return.
*
*
a lock tt> Implementation May Be Able To Detect
* Erroneous Use of the Lock, Such as an invocation That Would
* Cause Deadlock, and may throw an (unchecked) Exception in Such
* Circumstances. The Circumstances and The Exception Type Must
* be Document by That LOCK TT> IMPLEMENTATION.
*
* @Throws InterruptedException if The Current Thread is Interrupted
* While acquiring the lock (and interruption of lock acquisition is
* Supported).
*
* @see thread # interrupt
*
** /
Void LockInterruptibly () THROWS InterruptedException;
Method function:
Application lock unless the current application thread is interrupted.
Detailed Description:
If the lock is valid (not occupied by other threads); if the lock is invalid, apply for the lock thread (ie, called the current thread) will hang until a valid lock, There are two situations that occur at this time:
1. The current thread gets the lock.
2. The current thread is interrupted by other threads, and the interrupt of the lock application is supported. If the current thread is registered in the state of the thread being interrupted; or the interrupt of the lock application is supported, an interruptedException will be thrown, and the current thread's interrupt state will be cleared.
Considering the implementation: It is impossible to support the lock acquisition for some implementation, if it is possible to spend a lot of operation. Developers should pay attention to this fact. It is necessary to explain it when writing a document. A implementation supports returns based on interrupts on normal methods. Specific implementations are capable of detecting errors in use (such as deadlocks) and throw exceptions in accordance with the appropriate case. These specific abnormalities must be reflected in the implementation class of the LOCK. When the lock is being applied (if the interrupt of the application is supported), the current thread is interrupted, and an interrupt exception will be thrown.
/ **
* Acquires The Lock Only IT Is Free At The Time Of Invocation.
*
acquires the lock if it is available and returns immediately
* with the value True tt>.
* If The Lock Is Not Available The Method Will Return
* Immedierately with the value false tt>.
*
a type of usage idiom for this method 10o:
*
* Lock Lock = ...
* If (Lock.Trylock ()) {
* TRY {
* // manipulate protected state
*} Finally {
* Lock.unlock ();
*}
*} Else {
* // Perform Alternative ActionS
*}
* pre>
* This usage ensures that the Lock is unlocked if it is acquired, and
* Doesn't try to unlock if the lock was not acquired.
*
* @return True tt> if The lock Was acquired and false tt>
* Otherwise.
** /
Boolean trylock ();
Method function:
The lock is obtained only when the lock is valid (no thread occupies).
Detailed Description:
If the lock is valid, get the lock and return true immediately; otherwise return False immediately.
Typical use cases of this method:
Lock Lock = ...
IF (Lock.Trylock ()) {
Try {
// manipulate protected state
} finally {
Lock.unlock ();
}
} else {
// Perform Alternative ActionS
}
This usage ensures that if the lock is obtained, it will be unlocked, and if the lock is not obtained, do not try to unlock it.
/ **
* Acquires the Lock if it is free within the given waiting time and the
* Current Thread Has Not Been {@link thread # interrupt interrupted}.
*
*
if the lock is available this method returns immediately * with the value True tt>.
* If the lock is not available the
* The Current Thread Becomes Disabled for Thread Scheduling
* Purposes and lies dormant UnTil One of Three Things happens:
*
*
*
* thread, and interruption of lock acquisition is support; or
*
* ul>
*
if the lock is acquided the value true tt> is returned.
*
if the current thread:
*
*
*
* The Lock, And Interruption of Lock Acquisition Is Supported,
* ul>
* Ten {@Link InterruptedException} Is Thrown and the Current Thread's
* Interrupted status is cleared.
*
if the specified waiting time elapses dams the value false tt>
* is returned.
* If the time is
* Less Than or Equal to Zero, The Method Will Not Wait at all.
*
*
Implementation considances b>
*
The Ability to Interrupt a Lock Acquisition in Some IMPLEMENTATIONS
* May Not Be Possible, And if Possible May
* Be an expensive operation.
* The Programmer SHOULD BE Aware That this may be the case. AN
* IMPLEMENTATION SHOULD Document When this is the case.
*
an importation can favor Responding to an an interrupt over Normal
* Method return, or reporting a timeout.
*
a lock tt> Implementation May Be Able To Detect * Erroneous Use of the Lock, Such as an invocation That Would Cause
* DEADLOCK, And May Throw an (unchecked) Exception in Such Circumstances.
* The circumstances and the exception type must be docutented by That
* Lock TT> Implementation.
*
* @Param Time The maximum time to wait for the LOCK
* @Param Unit The Time Unit of the Time TT> Argument.
* @return True tt> if The lock Was acquired and false tt>
* if The Waiting Time Elapsed Before The Lock Was Acquired.
*
* @Throws InterruptedException if The Current Thread is Interrupted
* While acquiring the lock (and interruption of lock acquisition is
* Supported).
*
* @see thread # interrupt
*
** /
Boolean trylock (long time, timeunit unit) throws interruptedException;
Method function:
Get the lock within a given time
Detailed Description:
If the lock is valid at a certain wait time, and the current thread is not interrupted, the current thread will get the lock; if the lock is invalid, the current thread will hang until one of the following three cases occur:
1. Locks at a certain amount of time are obtained by the current thread;
2. Other threads interrupt the current thread, and the interrupt of the lock application is supported;
3. At a certain period of time, the current thread is not a lock, that is, the waiting time is exceeded for a given value.
When 1 occurs, the lock is obtained by the current thread and returns True;
When the case 2 occurs, if the interrupt state of the current thread is registered or the interrupt state of the current thread is registered, the interrupt of the lock application will be thrown, and the interrupt state of the current thread is cleared.
When 3 occurs, return false. If a given time period <= 0, the method will not wait.
Considering the implementation of the class:
Some interrupts that achieve support lock acquisition are unlikely, and a lot of operation will be paid if supported. Developers should pay attention to this situation. And implement the class must write this situation to a document. One implementation class can be interrupted and returned when performing a method, or reports timeout. Specific implementations are capable of detecting errors in use (such as deadlocks) and throw exceptions in accordance with the appropriate case. These specific abnormalities must be reflected in the implementation class of the LOCK. When the lock is being applied (if the interrupt of the application is supported), the current thread is interrupted, and an interrupt exception will be thrown.
Method Description:
Parameter 1: Waiting time (timeout)
Parameter 2: TimeUnit class object
Return Value: Returns true when the lock is obtained, and returns false when waiting for the timeout. Abnormal situation: If the current thread is interrupted when the lock is acquired (the interrupt of the lock is supported), the InterruptedException is thrown.
/ **
* Relevailazes the Lock.
*
Implementation considances b>
*
a lock tt> Implementation Will USUALLY IMPOSE
* Restrictions on Which Thread Can Release a Lock (Typically Only the
* Holder of the lock can release it) and may throw
* an (unchecked) Exception if The restriction is violated.
* Any Restrictions and The Exception
* TYPE MUST Be Docunted by Tt> Lock TT> Implementation.
** /
Void UNLOCK ();
Method function:
Release the lock.
Suggestions on implementation:
The lock implementation class can additional limits to threads that can release the lock, typically the lock holder can release the lock, and an exception will be thrown when the limit is violated. All restrictions and exceptions must be written to the document that implements the class.
/ **
* Returns a new {@link condition} instance tria is bound to this
* LOCK TT> Instance.
*
before waiting on the condition the lock must be held by the
* Current Thread.
* A call to {@Link Condition # await ()} Will Atomically Release The Lock
* Before Waiting and Re-Acquire the Lock Before The Wait Returns.
*
Implementation considances b>
*
The exact operation of the {@link condition} instance depends on the
* LOCK TT> Implementation and Must Be Docunted by That
* Implementation.
*
* @Return a new {@Link condition} instance for this lock tt>
* Instance.
* @Throws unsupportedOperationException if this lock tt>
* Implementation Does Not Support Conditions.
** /
Condition newcondition ();
Method function:
Returns a new condition instance that is bound to the lock.
Detailed Description:
The current thread must first obtain the lock before calling the method for the condition where the condition is returned. Before waiting to call the Condition.await method to automatically release the lock and reapply the lock before waiting for the return.
Suggestions for instances:
Conditional instance operations depend on the lock implementation, and must be written to the document of the instance class. Method Description:
Return Value: Returns a new condition instance that is bound to the lock.
Abnormal situation: If the lock does not support the condition, throw the unsupportedOperationException exception.
}
Summary, LOCK interface method list
Method Name Function Remarks Lock Get Lock If you do not get a lock, the current thread will hang until the lock is obtained. I call it "dead and other". LockInterruptibly gets a lock in a manner if the lock is not available, the current thread will hang until the lock or the current thread is interrupted by other threads. I referred to it as "pessimism and death". Trylock Attempts to get the lock If the lock is valid, the current thread gets the lock immediately and returns true, otherwise returns false. I call it "Try" to get trylock (long time, timeunit unit) Try to get the lock If the lock is valid, the current thread gets the lock immediately and returns True; otherwise waiting for a given time, if this time If you get a lock, returns true, otherwise returns false; otherwise like the LockInterruptibly, it is possible to be interrupted during the acquired process. I call it "Trial Timeout" to get ReleaseLock Release Lock
Newcondition Returns the condition of the current lock
Compare four Lock methods, "Trial" and "Test Exploration Timeout" are highly efficient. And "death, etc." and "pessimism and other" have low efficiency, and increase the resource overhead of the system when concurrent threads, and when there is no release of the lock, the thread that is waiting lock will be Have hang forever.