/** * Create a new instance. * * @param coreThreads the number of threads to create before enqueueing tasks * @param maxThreads the maximum number of threads to create * @param keepAliveTime the amount of time that an idle thread should remain active * @param keepAliveTimeUnit the unit of time for {@code keepAliveTime} * @param queue the queue to use for tasks * @param threadFactory the thread factory to use for new threads * @param blocking {@code true} if the executor should block when the queue is full and no threads are available, {@code false} to use the handoff executor * @param handoffExecutor the executor which is called when blocking is disabled and a task cannot be accepted, or {@code null} to reject the task */ public QueueExecutor(final int coreThreads, final int maxThreads, final long keepAliveTime, final TimeUnit keepAliveTimeUnit, final Queue<Runnable> queue, final ThreadFactory threadFactory, final boolean blocking, final Executor handoffExecutor) { this(coreThreads, maxThreads, keepAliveTime, keepAliveTimeUnit, queue, threadFactory, blocking, handoffExecutor, JBossExecutors.directExecutor()); }
@Override public ExecutorService apply(ExecutorService executor) { return JBossExecutors.protectedExecutorService(executor); }
/** * Create a direct executor which delegates tasks to the given executor, and then clears <b>all</b> thread-local * data after each task completes (regardless of outcome). You must have the {@link RuntimePermission}{@code ("modifyThread")} * permission to use this method. * * @param delegate the delegate direct executor * @return a resetting executor * @throws SecurityException if the caller does not have the {@link RuntimePermission}{@code ("modifyThread")} permission */ public static DirectExecutor resettingExecutor(final DirectExecutor delegate) throws SecurityException { return cleanupExecutor(delegate, threadLocalResetter()); }
/** * Create a thread factory which resets all thread-local storage and delegates to the given thread factory. * You must have the {@link RuntimePermission}{@code ("modifyThread")} permission to use this method. * * @param delegate the delegate thread factory * @return the resetting thread factory * @throws SecurityException if the caller does not have the {@link RuntimePermission}{@code ("modifyThread")} * permission */ public static ThreadFactory resettingThreadFactory(final ThreadFactory delegate) throws SecurityException { return wrappingThreadFactory(resettingExecutor(directExecutor()), delegate); }
/** * An executor which delegates to another executor, wrapping each task in a task wrapper. * * @param taskWrapper the task wrapper * @param delegate the delegate executor * @return a wrapping executor */ public static Executor wrappingExecutor(final DirectExecutor taskWrapper, final Executor delegate) { return executor(wrappingExecutor(delegate), taskWrapper); }
static DirectExecutor createTaskFilterRecursive(final Iterator<Object> it) { if (! it.hasNext()) { return JBossExecutors.directExecutor(); case THREAD_NAME: { final String nextString = (String) it.next(); return JBossExecutors.threadNameExecutor(createTaskFilterRecursive(it), nextString); return JBossExecutors.threadNameNotateExecutor(createTaskFilterRecursive(it), nextString); return JBossExecutors.exceptionLoggingExecutor(createTaskFilterRecursive(it), log, level); return JBossExecutors.resettingExecutor(createTaskFilterRecursive(it)); return JBossExecutors.cleanupExecutor(createTaskFilterRecursive(it), JBossExecutors.contextClassLoaderResetter()); return JBossExecutors.initializingExecutor(createTaskFilterRecursive(it), r); return JBossExecutors.cleanupExecutor(createTaskFilterRecursive(it), r);
/** * Run a task, invoking the given notifier with the given attachment. * * @param task the task * @param notifier the notifier * @param attachment the attachment * @param <R> the task type * @param <A> the attachment type */ public static <R extends Runnable, A> void run(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) { run(task, directExecutor(), notifier, attachment); }
public Thread newThread(final Runnable r) { return delegate.newThread(JBossExecutors.executorTask(taskWrapper, r)); }
/** * Create a task that delegates to the given task, preserving the context classloader which was in effect when * this method was invoked. * * @param delegate the delegate runnable * @return the wrapping runnable * @throws SecurityException if a security manager exists and the caller does not have the {@code "copyClassLoader"} * {@link RuntimePermission}. */ public static Runnable classLoaderPreservingTask(final Runnable delegate) throws SecurityException { final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(COPY_CONTEXT_CLASSLOADER_PERMISSION); } return classLoaderPreservingTaskUnchecked(delegate); }
/** * Create a direct executor which consumes and logs errors that are thrown to the default thread error category * {@code "org.jboss.threads.errors"}. * * @param delegate the executor to delegate to * @return the new direct executor */ public static DirectExecutor exceptionLoggingExecutor(final DirectExecutor delegate) { return exceptionLoggingExecutor(delegate, THREAD_ERROR_LOGGER); }
public static ThreadFactory createThreadFactory(final ThreadGroup threadGroup, final Boolean daemon, final Integer priority, final String namePattern, final Thread.UncaughtExceptionHandler exceptionHandler, final Long stackSize, final List<Object> taskFilterObjects) { return JBossExecutors.wrappingThreadFactory(createTaskFilter(taskFilterObjects), new JBossThreadFactory(threadGroup, daemon, priority, namePattern, exceptionHandler, stackSize)); }
public static Executor createJBossThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, List<Object> taskFilterObjects) { return JBossExecutors.wrappingExecutor(createTaskFilter(taskFilterObjects), new JBossThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory)); }
/** * Get a {@code Runnable} which, when executed, clears the thread-local storage of the calling thread. * You must have the {@link RuntimePermission}{@code ("modifyThread")} * permission to use this method. * * @return the runnable * @throws SecurityException if the caller does not have the {@link RuntimePermission}{@code ("modifyThread")} * permission */ public static Runnable threadLocalResetter() throws SecurityException { checkAccess(MODIFY_THREAD_PERMISSION); return THREAD_LOCAL_RESETTER; }
/** * Create a thread factory which resets all thread-local storage and delegates to the given thread factory. * You must have the {@link RuntimePermission}{@code ("modifyThread")} permission to use this method. * * @param delegate the delegate thread factory * @return the resetting thread factory * @throws SecurityException if the caller does not have the {@link RuntimePermission}{@code ("modifyThread")} * permission */ public static ThreadFactory resettingThreadFactory(final ThreadFactory delegate) throws SecurityException { return wrappingThreadFactory(resettingExecutor(directExecutor()), delegate); }
/** * Run a task, invoking the given notifier with the given attachment. * * @param task the task * @param notifier the notifier * @param attachment the attachment * @param <R> the task type * @param <A> the attachment type */ public static <R extends Runnable, A> void run(R task, TaskNotifier<? super R, ? super A> notifier, A attachment) { run(task, directExecutor(), notifier, attachment); }
/** * An executor which delegates to another executor, wrapping each task in a task wrapper. * * @param taskWrapper the task wrapper * @param delegate the delegate executor * @return a wrapping executor */ public static Executor wrappingExecutor(final DirectExecutor taskWrapper, final Executor delegate) { return executor(wrappingExecutor(delegate), taskWrapper); }
public void execute(final DirectExecutor directExecutor, final Runnable task) throws RejectedExecutionException { execute(JBossExecutors.executorTask(directExecutor, task)); } }
final Runnable realRunnable = JBossExecutors.classLoaderPreservingTaskUnchecked(runnable); int result; if (TAIL_LOCK) synchronized (tailLock) {
/** * Create a direct executor which consumes and logs errors that are thrown to the default thread error category * {@code "org.jboss.threads.errors"}. * * @param delegate the executor to delegate to * @return the new direct executor */ public static DirectExecutor exceptionLoggingExecutor(final DirectExecutor delegate) { return exceptionLoggingExecutor(delegate, THREAD_ERROR_LOGGER); }
/** * Get a {@code Runnable} which, when executed, clears the thread-local storage of the calling thread. * You must have the {@link RuntimePermission}{@code ("modifyThread")} * permission to use this method. * * @return the runnable * @throws SecurityException if the caller does not have the {@link RuntimePermission}{@code ("modifyThread")} * permission */ public static Runnable threadLocalResetter() throws SecurityException { checkAccess(MODIFY_THREAD_PERMISSION); return THREAD_LOCAL_RESETTER; }