Top Description Inners Fields Constructors Methods
org.apache.tomcat.util.threads

public Class LimitLatch

extends Object
Class Inheritance
Imports
java.util.Collection, java.util.concurrent.atomic.AtomicLong, java.util.concurrent.locks.AbstractQueuedSynchronizer, org.apache.juli.logging.Log, .LogFactory

Shared latch that allows the latch to be acquired a limited number of times after which all subsequent requests to acquire the latch will be placed in a FIFO queue until one of the shares is returned.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private final AtomicLong
private volatile long
private static final Log
private volatile boolean
private final LimitLatch.Sync

Constructor Summary

AccessConstructor and Description
public
LimitLatch(long
- maximum number of concurrent acquisitions of this latch
limit
)

Instantiates a LimitLatch object with an initial limit.

Method Summary

Modifier and TypeMethod and Description
public long

Returns:

the previous counter value
countDown
()

Releases a shared latch, making it available for another thread to use.

public void
countUpOrAwait()

Acquires a shared latch if one is available or waits for one if no shared latch is current available.

public long

Returns:

the current count for latch
getCount
()

Returns the current count for the latch

public long

Returns:

the limit
getLimit
()

Obtain the current limit.

public Collection<Thread>

Returns:

a collection of threads
getQueuedThreads
()

Provide access to the list of threads waiting to acquire this limited shared latch.

public boolean

Returns:

true if threads are waiting
hasQueuedThreads
()

Returns true if there is at least one thread waiting to acquire the shared lock, otherwise returns false.

public boolean

Returns:

true if release was done
releaseAll
()

Releases all waiting threads and causes the limit to be ignored until reset() is called.

public void
reset()

Resets the latch and initializes the shared acquisition counter to zero.

public void
setLimit(long
The new limit
limit
)

Sets a new limit.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

countback to summary
private final AtomicLong count
limitback to summary
private volatile long limit
logback to summary
private static final Log log
releasedback to summary
private volatile boolean released
syncback to summary
private final LimitLatch.Sync sync

Constructor Detail

LimitLatchback to summary
public LimitLatch(long limit)

Instantiates a LimitLatch object with an initial limit.

Parameters
limit:long

- maximum number of concurrent acquisitions of this latch

Method Detail

countDownback to summary
public long countDown()

Releases a shared latch, making it available for another thread to use.

Returns:long

the previous counter value

countUpOrAwaitback to summary
public void countUpOrAwait() throws InterruptedException

Acquires a shared latch if one is available or waits for one if no shared latch is current available.

Exceptions
InterruptedException:
If the current thread is interrupted
getCountback to summary
public long getCount()

Returns the current count for the latch

Returns:long

the current count for latch

getLimitback to summary
public long getLimit()

Obtain the current limit.

Returns:long

the limit

getQueuedThreadsback to summary
public Collection<Thread> getQueuedThreads()

Provide access to the list of threads waiting to acquire this limited shared latch.

Returns:Collection<Thread>

a collection of threads

hasQueuedThreadsback to summary
public boolean hasQueuedThreads()

Returns true if there is at least one thread waiting to acquire the shared lock, otherwise returns false.

Returns:boolean

true if threads are waiting

releaseAllback to summary
public boolean releaseAll()

Releases all waiting threads and causes the limit to be ignored until reset() is called.

Returns:boolean

true if release was done

resetback to summary
public void reset()

Resets the latch and initializes the shared acquisition counter to zero.

See Also
releaseAll()
setLimitback to summary
public void setLimit(long limit)

Sets a new limit. If the limit is decreased there may be a period where more shares of the latch are acquired than the limit. In this case no more shares of the latch will be issued until sufficient shares have been returned to reduce the number of acquired shares of the latch to below the new limit. If the limit is increased, threads currently in the queue may not be issued one of the newly available shares until the next request is made for a latch.

Parameters
limit:long

The new limit

org.apache.tomcat.util.threads back to summary

private Class LimitLatch.Sync

extends AbstractQueuedSynchronizer
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
Sync()

Method Summary

Modifier and TypeMethod and Description
protected int
tryAcquireShared(int
the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
ignored
)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Attempts to acquire in shared mode.

protected boolean
tryReleaseShared(int
the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
arg
)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Attempts to set the state to reflect a release in shared mode.

Inherited from java.util.concurrent.locks.AbstractQueuedSynchronizer:
acquireacquireInterruptiblyacquireSharedacquireSharedInterruptiblycompareAndSetStategetExclusiveQueuedThreadsgetFirstQueuedThreadgetQueuedThreadsgetQueueLengthgetSharedQueuedThreadsgetStategetWaitingThreadsgetWaitQueueLengthhasContendedhasQueuedPredecessorshasQueuedThreadshasWaitersisHeldExclusivelyisQueuedownsreleasereleaseSharedsetStatetoStringtryAcquiretryAcquireNanostryAcquireSharedNanostryRelease

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.locks.AbstractQueuedSynchronizer.serialVersionUID.

Constructor Detail

Syncback to summary
pack-priv Sync()

Method Detail

tryAcquireSharedback to summary
protected int tryAcquireShared(int ignored)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Doc from java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Attempts to acquire in shared mode. This method should query if the state of the object permits it to be acquired in the shared mode, and if so to acquire it.

This method is always invoked by the thread performing acquire. If this method reports failure, the acquire method may queue the thread, if it is not already queued, until it is signalled by a release from some other thread.

The default implementation throws UnsupportedOperationException.

Parameters
ignored:int

the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.

Returns:int

a negative value on failure; zero if acquisition in shared mode succeeded but no subsequent shared-mode acquire can succeed; and a positive value if acquisition in shared mode succeeded and subsequent shared-mode acquires might also succeed, in which case a subsequent waiting thread must check availability. (Support for three different return values enables this method to be used in contexts where acquires only sometimes act exclusively.) Upon success, this object has been acquired.

Annotations
@Override
tryReleaseSharedback to summary
protected boolean tryReleaseShared(int arg)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Doc from java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Attempts to set the state to reflect a release in shared mode.

This method is always invoked by the thread performing release.

The default implementation throws UnsupportedOperationException.

Parameters
arg:int

the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.

Returns:boolean

true if this release of shared mode may permit a waiting acquire (shared or exclusive) to succeed; and false otherwise

Annotations
@Override