Example usage for java.util.concurrent ScheduledThreadPoolExecutor setExecuteExistingDelayedTasksAfterShutdownPolicy

List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor setExecuteExistingDelayedTasksAfterShutdownPolicy

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledThreadPoolExecutor setExecuteExistingDelayedTasksAfterShutdownPolicy.

Prototype

public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) 

Source Link

Document

Sets the policy on whether to execute existing delayed tasks even when this executor has been shutdown .

Usage

From source file:Main.java

/**
 * Have shutdown actually means shutdown. Tasks that need to complete should use
 * futures.//w  w w. j ava  2  s. com
 */
public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name,
        UncaughtExceptionHandler handler, int poolSize, int stackSize) {
    // HACK: ScheduledThreadPoolExecutor won't let use the handler so
    // if we're using ExceptionHandlingRunnable then we'll be able to 
    // pick up the exceptions
    Thread.setDefaultUncaughtExceptionHandler(handler);

    ThreadFactory factory = getThreadFactory(name, handler);
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(poolSize, factory);
    executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    return executor;
}

From source file:com.l2jfree.util.concurrent.L2ThreadPool.java

public static void shutdown() {
    final long begin = System.currentTimeMillis();

    try {//from   www  .  j  a  v a2s .  com
        System.out.println("L2ThreadPool: Shutting down.");
        System.out.println("\t... executing " + getTaskCount(_scheduledPools) + " scheduled tasks.");
        System.out.println("\t... executing " + getTaskCount(_instantPools) + " instant tasks.");
        System.out.println("\t... executing " + getTaskCount(_longRunningPools) + " long running tasks.");
    } catch (Throwable t) {
        t.printStackTrace();
    }

    try {
        for (ThreadPoolExecutor threadPool : getThreadPools()) {
            try {
                threadPool.shutdown();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    boolean success = false;
    try {
        success |= awaitTermination(5000);

        for (ScheduledThreadPoolExecutor scheduledPool : _scheduledPools) {
            scheduledPool.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
            scheduledPool.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
        }

        success |= awaitTermination(10000);
    } catch (Throwable t) {
        t.printStackTrace();
    }

    try {
        System.out.println(
                "\t... success: " + success + " in " + (System.currentTimeMillis() - begin) + " msec.");
        System.out.println("\t... " + getTaskCount(_scheduledPools) + " scheduled tasks left.");
        System.out.println("\t... " + getTaskCount(_instantPools) + " instant tasks left.");
        System.out.println("\t... " + getTaskCount(_longRunningPools) + " long running tasks left.");

        if (TimeUnit.MINUTES.toMillis(30) < ManagementFactory.getRuntimeMXBean().getUptime())
            RunnableStatsManager.dumpClassStats(SortBy.TOTAL);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:io.bitsquare.common.util.Utilities.java

public static ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor(String name, int corePoolSize,
        int maximumPoolSize, long keepAliveTimeInSec) {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(name).setDaemon(true)
            .setPriority(Thread.MIN_PRIORITY).build();
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    executor.setKeepAliveTime(keepAliveTimeInSec, TimeUnit.SECONDS);
    executor.allowCoreThreadTimeOut(true);
    executor.setMaximumPoolSize(maximumPoolSize);
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setRejectedExecutionHandler((r, e) -> {
        log.debug("RejectedExecutionHandler called");
    });//  w w w. j  a  va 2s  .  c  om
    return executor;
}

From source file:org.mule.config.pool.DefaultThreadPoolFactory.java

protected ScheduledThreadPoolExecutor internalCreateScheduledPool(ThreadingProfile tp) {
    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
            Math.min(tp.getMaxThreadsIdle(), tp.getMaxThreadsActive()));
    scheduledThreadPoolExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    scheduledThreadPoolExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
    scheduledThreadPoolExecutor.setKeepAliveTime(tp.getThreadTTL(), TimeUnit.MILLISECONDS);
    scheduledThreadPoolExecutor.setCorePoolSize(tp.getMaxThreadsIdle());
    scheduledThreadPoolExecutor.setMaximumPoolSize(tp.getMaxThreadsActive());
    return scheduledThreadPoolExecutor;
}

From source file:org.mule.transport.AbstractConnector.java

protected ScheduledExecutorService createScheduler() {
    // Use connector's classloader so that other temporary classloaders
    // aren't used when things are started lazily or from elsewhere.
    ThreadFactory threadFactory = new NamedThreadFactory(this.getName() + ".scheduler",
            muleContext.getExecutionClassLoader());
    ScheduledThreadPoolExecutor newExecutor = new ScheduledThreadPoolExecutor(4, threadFactory);
    newExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    newExecutor.setKeepAliveTime(this.getReceiverThreadingProfile().getThreadTTL(), TimeUnit.MILLISECONDS);
    newExecutor.allowCoreThreadTimeOut(true);
    return newExecutor;
}