Top Description Inners Fields Constructors Methods
org.jboss.threads

pack-priv final Class EnhancedViewExecutor

extends ViewExecutor
Class Inheritance
Imports
org.jboss.logging.Logger, org.wildfly.common.Assert, org.wildfly.common.annotation.Nullable, org.wildfly.common.cpu.ProcessorInfo, org.wildfly.common.lock.Locks, java.util.ArrayList, .Collections, .List, .Queue, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue, .Executor, .RejectedExecutionException, .TimeUnit, java.util.concurrent.locks.Lock

A View Executor implementation which avoids lock contention in the common path. This allows us to provide references to the same underlying pool of threads to different consumers and utilize distinct instrumentation without duplicating resources. This implementation is optimized to avoid locking.
Author
Carter Kozak

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private static final long
private final Set<EnhancedViewExecutor.EnhancedViewExecutorRunnable>
private final Executor
private final Lock
executeLock

The execute lock is only needed necessary to guard from submitting the first queued task before the last non-queued (active == maxCount) task can be submitted to the executor.

private volatile boolean
private static final Logger
private static final int
private final int
private final Queue<EnhancedViewExecutor.EnhancedViewExecutorRunnable>
queue

Queue handling: The queue must only be modified after a successful CAS state update.

private static final int
private static final long
private static final int
private final int
private static final long
private final Object
private volatile long
state

State structure.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
EnhancedViewExecutor(final Executor delegate, final int maxCount, final int queueLimit, final Thread.UncaughtExceptionHandler uncaughtExceptionHandler)

Method Summary

Modifier and TypeMethod and Description
public boolean
awaitTermination(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Implements java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
private EnhancedViewExecutor.EnhancedViewExecutorRunnable
blockingTake()

Tasks are enqueued after a successful state update, they may not be immediately available at this point.

private boolean
compareAndSwapState(long expected, long update)

private boolean
private void
enqueue(EnhancedViewExecutor.EnhancedViewExecutorRunnable task)

Only called after the queue size component of state has been updated.

public void
execute(Runnable
the runnable task
task
)

Implements java.util.concurrent.Executor.execute.

Executes the given command at some time in the future.
private static int
getActiveCount(long state)

private static int
getQueueSize(long state)

public boolean
isShutdown()

Implements java.util.concurrent.ExecutorService.isShutdown.

Returns true if this executor has been shut down.
private static boolean
isShutdown(long state)

public boolean
isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down.
private static boolean
isTerminated(long state)

private void
notifyWaitersIfTerminated(long stateSnapshot)

public void
shutdown(boolean interrupt)

Implements abstract org.jboss.threads.ViewExecutor.shutdown.

public List<Runnable>
shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
private EnhancedViewExecutor.EnhancedViewExecutorRunnable
taskComplete(boolean allowQueuePolling)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private Thread.UncaughtExceptionHandler
Inherited from org.jboss.threads.ViewExecutor:
buildergetExceptionHandlergetTerminationTaskreadIntPropertyPrefixedreadPropertyreadPropertyPrefixedreadPropertyRawrunTerminationsetExceptionHandlersetTerminationTaskshutdown

Field Detail

ACTIVE_COUNT_MASKback to summary
private static final long ACTIVE_COUNT_MASK
activeRunnablesback to summary
private final Set<EnhancedViewExecutor.EnhancedViewExecutorRunnable> activeRunnables
delegateback to summary
private final Executor delegate
executeLockback to summary
private final Lock executeLock

The execute lock is only needed necessary to guard from submitting the first queued task before the last non-queued (active == maxCount) task can be submitted to the executor. Locks are not used when the queue is disabled. This lock is required to prevent tasks from beginning to queue while tasks that have acquired permits have not been successfully submitted to the delegate executor. In this case the queue may be left in an unrecoverable state if the delegate executor rejects input.

Annotations
@Nullable
interruptedback to summary
private volatile boolean interrupted
logback to summary
private static final Logger log
MAX_QUEUE_SPINSback to summary
private static final int MAX_QUEUE_SPINS
maxCountback to summary
private final int maxCount
queueback to summary
private final Queue<EnhancedViewExecutor.EnhancedViewExecutorRunnable> queue

Queue handling: The queue must only be modified after a successful CAS state update. After the queue length component of state has been decremented a receiver may need to wait for the producer to successfully add an item to the queue.

QUEUE_FAILURE_LOG_INTERVALback to summary
private static final int QUEUE_FAILURE_LOG_INTERVAL
QUEUED_SIZE_MASKback to summary
private static final long QUEUED_SIZE_MASK
QUEUED_SIZE_OFFSETback to summary
private static final int QUEUED_SIZE_OFFSET
queueLimitback to summary
private final int queueLimit
SHUTDOWN_MASKback to summary
private static final long SHUTDOWN_MASK
shutdownLockback to summary
private final Object shutdownLock
stateback to summary
private volatile long state

State structure.

  • Bit 00..30: Number of active tasks (unsigned)
  • Bit 31..61: Number of queued tasks (unsigned)
  • Bit 62: unused; always zero
  • Bit 63: executor shutdown state; 0 = shutdown has not been requested
Annotations
@SuppressWarnings:unused
stateOffsetback to summary
private static final long stateOffset

Constructor Detail

EnhancedViewExecutorback to summary
pack-priv EnhancedViewExecutor(final Executor delegate, final int maxCount, final int queueLimit, final Thread.UncaughtExceptionHandler uncaughtExceptionHandler)

Method Detail

awaitTerminationback to summary
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.ExecutorService.awaitTermination.

Doc from java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Parameters
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:boolean

true if this executor terminated and false if the timeout elapsed before termination

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting
blockingTakeback to summary
private EnhancedViewExecutor.EnhancedViewExecutorRunnable blockingTake()

Tasks are enqueued after a successful state update, they may not be immediately available at this point.

compareAndSwapStateback to summary
private boolean compareAndSwapState(long expected, long update)
decrementQueueSizeback to summary
private boolean decrementQueueSize()
enqueueback to summary
private void enqueue(EnhancedViewExecutor.EnhancedViewExecutorRunnable task)

Only called after the queue size component of state has been updated. At this point there may already be a thread waiting for the queued task after decrementing the queued count, so it's vital that this operation succeeds.

executeback to summary
public void execute(Runnable task)

Implements java.util.concurrent.Executor.execute.

Doc from java.util.concurrent.Executor.execute.

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

Parameters
task:Runnable

the runnable task

Annotations
@Override
getActiveCountback to summary
private static int getActiveCount(long state)
getQueueSizeback to summary
private static int getQueueSize(long state)
isShutdownback to summary
public boolean isShutdown()

Implements java.util.concurrent.ExecutorService.isShutdown.

Doc from java.util.concurrent.ExecutorService.isShutdown.

Returns true if this executor has been shut down.

Returns:boolean

true if this executor has been shut down

Annotations
@Override
isShutdownback to summary
private static boolean isShutdown(long state)
isTerminatedback to summary
public boolean isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Doc from java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.

Returns:boolean

true if all tasks have completed following shut down

Annotations
@Override
isTerminatedback to summary
private static boolean isTerminated(long state)
notifyWaitersIfTerminatedback to summary
private void notifyWaitersIfTerminated(long stateSnapshot)
shutdownback to summary
public void shutdown(boolean interrupt)

Implements abstract org.jboss.threads.ViewExecutor.shutdown.

Annotations
@Override
shutdownNowback to summary
public List<Runnable> shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Doc from java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread#interrupt, so any task that fails to respond to interrupts may never terminate.

Returns:List<Runnable>

list of tasks that never commenced execution

Annotations
@Override
taskCompleteback to summary
private EnhancedViewExecutor.EnhancedViewExecutorRunnable taskComplete(boolean allowQueuePolling)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
uncaughtExceptionHandlerback to summary
private Thread.UncaughtExceptionHandler uncaughtExceptionHandler()
org.jboss.threads back to summary

private final Class EnhancedViewExecutor.EnhancedViewExecutorRunnable

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.jboss.threads.EnhancedViewExecutor.EnhancedViewExecutorRunnable
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private ClassLoader
private Runnable
private volatile Thread

Constructor Summary

AccessConstructor and Description
pack-priv
EnhancedViewExecutorRunnable(Runnable delegate, ClassLoader contextClassLoader)

Method Summary

Modifier and TypeMethod and Description
pack-priv synchronized void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

contextClassLoaderback to summary
private ClassLoader contextClassLoader
delegateback to summary
private Runnable delegate
threadback to summary
private volatile Thread thread
Annotations
@Nullable

Constructor Detail

EnhancedViewExecutorRunnableback to summary
pack-priv EnhancedViewExecutorRunnable(Runnable delegate, ClassLoader contextClassLoader)

Method Detail

interruptback to summary
pack-priv synchronized void interrupt()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override