private Thread createThread(final Runnable target) { final ThreadNameInfo nameInfo = new ThreadNameInfo(globalThreadIndexSequence.getAndIncrement(), factoryThreadIndexSequence.getAndIncrement(), factoryIndex); final JBossThread thread; if (stackSize != null) { thread = new JBossThread(threadGroup, target, "<new>", stackSize.longValue()); } else { thread = new JBossThread(threadGroup, target); } thread.setThreadNameInfo(nameInfo); thread.setName(nameInfo.format(thread, namePattern)); if (initialPriority != null) thread.setPriority(initialPriority.intValue()); if (daemon != null) thread.setDaemon(daemon.booleanValue()); if (uncaughtExceptionHandler != null) thread.setUncaughtExceptionHandler(uncaughtExceptionHandler); JBossExecutors.clearContextClassLoader(thread); return thread; } }
public static <T, E extends Exception> void acceptInterruptDeferredEx(final ExceptionConsumer<T, E> consumer, T param) throws E { acceptInterruptDeferredEx(Functions.exceptionConsumerBiConsumer(), consumer, param); }
public static <T, E extends Exception> void acceptInterruptResumedEx(final ExceptionConsumer<T, E> consumer, T param) throws E { acceptInterruptResumedEx(Functions.exceptionConsumerBiConsumer(), consumer, param); }
public void execute(final Runnable command) { final JBossThread thr = JBossThread.currentThread(); final String oldName = thr.getName(); thr.setName(thr.getThreadNameInfo().format(thr, nameFormat)); try { delegate.execute(command); } finally { thr.setName(oldName); } }
/** * Defer interrupts for the duration of some task. Once the task is complete, any deferred interrupt will be * delivered to the thread, thus the thread interrupt status should be checked upon return. If the current thread * is not a {@code JBossThread}, the task is simply run as-is. * * @param action the task to run * @param <T> the callable's return type * @return the value returned from the callable * @throws Exception if the action throws an exception */ public static <T> T executeWithInterruptDeferred(final Callable<T> action) throws Exception { final JBossThread thread = currentThread(); if (registerDeferral(thread)) try { return action.call(); } finally { unregisterDeferral(thread); } else { // already deferred return action.call(); } }
if (differentThread) checkAccess(); if (isInterrupted()) return; final AtomicInteger stateRef = this.stateRef; int oldVal, newVal; doInterrupt(); } finally {
/** * Swap the current thread's active interrupt handler. Most callers should restore the old handler in a {@code finally} * block like this: * <pre> * InterruptHandler oldHandler = JBossThread.getAndSetInterruptHandler(newHandler); * try { * ...execute interrupt-sensitive operation... * } finally { * JBossThread.getAndSetInterruptHandler(oldHandler); * } * </pre> * * @param newInterruptHandler the new interrupt handler * @return the old interrupt handler */ public static InterruptHandler getAndSetInterruptHandler(final InterruptHandler newInterruptHandler) { final JBossThread thread = currentThread(); if (thread == null) { throw Messages.msg.noInterruptHandlers(); } try { return thread.interruptHandler; } finally { thread.interruptHandler = newInterruptHandler; } }
public static <T, R, E extends Exception> R applyWithInterruptHandler(InterruptHandler interruptHandler, ExceptionFunction<T, R, E> function, T param1) throws E { return applyWithInterruptHandler(interruptHandler, Functions.exceptionFunctionBiFunction(), function, param1); }
public static <T, R, E extends Exception> R applyInterruptDeferredEx(final ExceptionFunction<T, R, E> function, T param) throws E { return applyInterruptDeferredEx(Functions.exceptionFunctionBiFunction(), function, param); }
public static <T, R, E extends Exception> R applyInterruptResumedEx(final ExceptionFunction<T, R, E> function, T param) throws E { return applyInterruptResumedEx(Functions.exceptionFunctionBiFunction(), function, param); }
public static <T, E extends Exception> void acceptWithInterruptHandler(InterruptHandler interruptHandler, ExceptionConsumer<T, E> function, T param1) throws E { acceptWithInterruptHandler(interruptHandler, Functions.exceptionConsumerBiConsumer(), function, param1); }
/** * Set the thread name information. This includes information about the thread's sequence number and so forth. * * @param threadNameInfo the new thread name info * @throws SecurityException if the calling thread is not allowed to modify this thread */ void setThreadNameInfo(final ThreadNameInfo threadNameInfo) throws SecurityException { checkAccess(); this.threadNameInfo = threadNameInfo; } }
private static boolean unregisterDeferral(final JBossThread thread) { if (thread == null) { return false; } int oldVal, newVal; final AtomicInteger stateRef = thread.stateRef; do { oldVal = stateRef.get(); if (oldVal == STATE_MAYBE_INTERRUPTED || oldVal == STATE_INTERRUPT_IN_PROGRESS) { // already not deferred return false; } else if (oldVal == STATE_INTERRUPT_DEFERRED) { newVal = STATE_MAYBE_INTERRUPTED; } else if (oldVal == STATE_INTERRUPT_PENDING) { newVal = STATE_INTERRUPT_IN_PROGRESS; } else { throw Assert.unreachableCode(); } } while (! stateRef.compareAndSet(oldVal, newVal)); if (newVal == STATE_INTERRUPT_IN_PROGRESS) try { thread.doInterrupt(); } finally { stateRef.set(STATE_MAYBE_INTERRUPTED); } return true; }
public static <T, E extends Exception> void acceptInterruptDeferredEx(final ExceptionObjIntConsumer<T, E> consumer, T param1, int param2) throws E { final JBossThread thread = currentThread(); if (registerDeferral(thread)) try { consumer.accept(param1, param2); } finally { unregisterDeferral(thread); } else { // already deferred consumer.accept(param1, param2); } }
public void execute(final Runnable command) { final JBossThread thr = JBossThread.currentThread(); final String oldName = thr.getName(); thr.setName(thr.getThreadNameInfo().format(thr, nameFormat)); try { delegate.execute(command); } finally { thr.setName(oldName); } }
if (differentThread) checkAccess(); if (isInterrupted()) return; final AtomicInteger stateRef = this.stateRef; int oldVal, newVal; doInterrupt(); } finally {
public static <T, U, R, E extends Exception> R applyWithInterruptHandler(InterruptHandler interruptHandler, ExceptionBiFunction<T, U, R, E> function, T param1, U param2) throws E { final JBossThread thread = currentThread(); if (thread == null) { return function.apply(param1, param2); } else { final InterruptHandler old = thread.interruptHandler; thread.interruptHandler = interruptHandler; try { return function.apply(param1, param2); } finally { thread.interruptHandler = old; } } }
public static <R, E extends Exception> R getWithInterruptHandler(InterruptHandler interruptHandler, ExceptionSupplier<R, E> function) throws E { return applyWithInterruptHandler(interruptHandler, Functions.exceptionFunctionBiFunction(), Functions.exceptionSupplierFunction(), function); }
public static <T, E extends Exception> T getInterruptDeferredEx(final ExceptionSupplier<T, E> supplier) throws E { return applyInterruptDeferredEx(Functions.exceptionFunctionBiFunction(), Functions.exceptionSupplierFunction(), supplier); }
public static <T, E extends Exception> T getInterruptResumedEx(final ExceptionSupplier<T, E> supplier) throws E { return applyInterruptResumedEx(Functions.exceptionFunctionBiFunction(), Functions.exceptionSupplierFunction(), supplier); }