Class CompeteLatch
Object
com.liferay.portal.kernel.concurrent.CompeteLatch
A synchronizer based on the JDK's AQS framework to simulate a single winner
competition. This synchronizer supports cyclical competition. In this
situation, loser threads should try again. The single winner thread will lock
the latch while other threads will block on the latch by calling
await. After the winner thread finishes its job, it should call
done which will open the latch. All blocking loser threads can
pass the latch at the same time.
See LPS-3744 for a sample use case.
- Author:
- Shuyang Zhou
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidawait()This method should only be called by a loser thread.booleanThis method should only be called by a loser thread.booleancompete()Tells the current thread to join the competition.booleandone()This method should only be called by the winner thread.booleanisLocked()Returnstrueif the latch is locked.
-
Constructor Details
-
CompeteLatch
public CompeteLatch()
-
-
Method Details
-
await
This method should only be called by a loser thread. If the latch is locked, that means the winner is executing its job and all loser threads that call this method will be blocked. If the latch is not locked, that means the winner has finished its job and all the loser threads calling this method will return immediately. If the winner thread calls this method before his job completed, then all threads will deadlock.- Throws:
InterruptedException- if the current thread is interrupted
-
await
This method should only be called by a loser thread. If the latch is locked, that means the winner is executing its job and all loser threads that call this method will be blocked for the given waiting time. If the latch is not locked, that means the winner has finished its job and all the loser threads calling this method will return immediately. If the winner thread calls this method before his job completed, then all threads will deadlock.- Parameters:
timeout- the timeout valuetimeUnit- the time unit- Returns:
trueif the latch was open,falseif the waiting time elapsed before the latch be opened.- Throws:
InterruptedException- if the current thread is interrupted
-
compete
public boolean compete()Tells the current thread to join the competition. Return immediately whether or not the current thread is the winner thread or a loser thread. No matter how many threads join this competition, only one thread can be the winner thread.- Returns:
trueif the current thread is the winner thread
-
done
public boolean done()This method should only be called by the winner thread. The winner thread calls this method to indicate that it has finished its job and unlocks the latch to allow all loser threads return from theawaitmethod. If a loser thread does call this method when a winner thread has locked the latch, the latch will break and the winner thread may be put into a non thread safe state. You should never have to do this except to get out of a deadlock. If no one threads have locked the latch, then calling this method has no effect. This method will return immediately.- Returns:
trueif this call opens the latch,falseif the latch is already open
-
isLocked
public boolean isLocked()Returnstrueif the latch is locked. This method should not be used to test the latch before joining a competition because it is not thread safe. The only purpose for this method is to give external systems a way to monitor the latch which is usually be used for deadlock detection.- Returns:
trueif the latch is locked;falseotherwise
-