Example usage for java.util.concurrent ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor

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

Introduction

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

Prototype

public ScheduledThreadPoolExecutor(int corePoolSize, RejectedExecutionHandler handler) 

Source Link

Document

Creates a new ScheduledThreadPoolExecutor with the given initial parameters.

Usage

From source file:org.janusgraph.TestBed.java

/**
 * @param args//  www . j  av a2  s. c  om
 * @throws java.io.IOException
 */
public static void main(String[] args) throws Exception {
    Method method = TestBed.class.getMethod("getInt", int.class, int.class);
    AnnotatedType rt = method.getAnnotatedReturnType();
    System.out.println(rt.getType());
    System.out.println(rt.getAnnotations().length);
    System.out.println(method.getAnnotations().length);
    for (int i = 0; i < method.getAnnotations().length; i++) {
        System.out.println(method.getAnnotations()[i]);
    }

    //        String[] s = {"a","b","c","d","e","f","g","h","i","x","u"};
    //        int len = s.length;
    //        Random random = new Random();
    //
    //        Context c = new Context(new ObserverManager(),Observer.NO_OP);
    //        //Warmup
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long before = System.nanoTime();
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long total = System.nanoTime()-before;
    //        System.out.println("Total time: " + total/1000000);

    System.exit(0);

    final ScheduledExecutorService exe = new ScheduledThreadPoolExecutor(1, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            r.run();
        }
    });
    ScheduledFuture future = exe.scheduleWithFixedDelay(new Runnable() {
        AtomicInteger atomicInt = new AtomicInteger(0);

        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    exe.submit(new Runnable() {

                        private final int number = atomicInt.incrementAndGet();

                        @Override
                        public void run() {
                            try {
                                Thread.sleep(150);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(number);
                        }
                    });
                    System.out.println("Submitted: " + i);
                    //                    doSomethingExpensive(20);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1, TimeUnit.SECONDS);
    Thread.sleep(10000);
    //        future.get(1,TimeUnit.SECONDS);
    System.out.println("Cancel: " + future.cancel(false));
    System.out.println("Done: " + future.isDone());
    exe.shutdown();
    //        Thread.sleep(2000);
    System.out.println("Terminate: " + exe.awaitTermination(5, TimeUnit.SECONDS));
    System.out.println("DONE");
    NonBlockingHashMapLong<String> id1 = new NonBlockingHashMapLong<String>(128);
    ConcurrentHashMap<Long, String> id2 = new ConcurrentHashMap<Long, String>(128, 0.75f, 2);

}

From source file:com.addthis.codec.utils.ScheduledExecutorServiceBuilder.java

public ScheduledExecutorService build() {
    ScheduledThreadPoolExecutor service = new ScheduledThreadPoolExecutor(coreThreads, threadFactory);
    if (shutdownHook) {
        return MoreExecutors.getExitingScheduledExecutorService(service);
    } else {//from www  . j  av  a 2s .  c  o  m
        return service;
    }
}

From source file:Main.java

/**
 * Have shutdown actually means shutdown. Tasks that need to complete should use
 * futures.//from  www .j ava  2s  . c om
 */
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:org.apache.tajo.worker.DeletionService.java

public DeletionService(int defaultThreads, int debugDelay) {
    ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("DeletionService #%d").build();

    sched = new ScheduledThreadPoolExecutor(defaultThreads, tf);
    sched.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    sched.setKeepAliveTime(60L, TimeUnit.SECONDS);
    this.debugDelay = debugDelay;
}

From source file:org.hyperic.util.thread.ThreadWatchdog.java

/**
 * Must be called prior to any other use of this object.
 *///from www  .j  a  v  a 2 s. c  om
@PostConstruct
public void initialize() {
    _executor = new ScheduledThreadPoolExecutor(1, _tFact);
}

From source file:com.example.dwr.ticketcenter.CallCenter.java

/**
 * Create a new publish thread and start it
 *//* www . jav a  2  s .c o m*/
public CallCenter() {
    // Start with some calls waiting
    addRandomKnownCall();
    addRandomUnknownCall();
    addRandomUnknownCall();
    addRandomUnknownCall();

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory());
    //noinspection ThisEscapedInObjectConstruction
    executor.scheduleAtFixedRate(this, 2, 2, TimeUnit.SECONDS);
}

From source file:io.stallion.monitoring.HealthTracker.java

public static void start() {
    BasicThreadFactory factory = new BasicThreadFactory.Builder()
            .namingPattern("stallion-health-tracker-thread-%d").build();
    instance().timedChecker = new ScheduledThreadPoolExecutor(2, factory);
    instance().timedChecker.scheduleAtFixedRate(instance().metrics, 0, 1, TimeUnit.MINUTES);
    instance().timedChecker.scheduleAtFixedRate(instance().dailyMetrics, 0, 24 * 60, TimeUnit.MINUTES);
}

From source file:io.joynr.messaging.MessagingModule.java

@Provides
@Named(MessageScheduler.SCHEDULEDTHREADPOOL)
ScheduledExecutorService provideMessageSchedulerThreadPoolExecutor(
        @Named(ConfigurableMessagingSettings.PROPERTY_MESSAGING_MAXIMUM_PARALLEL_SENDS) int maximumParallelSends) {
    ThreadFactory schedulerNamedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("joynr.MessageScheduler-scheduler-%d").build();
    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(maximumParallelSends,
            schedulerNamedThreadFactory);
    scheduler.setKeepAliveTime(100, TimeUnit.SECONDS);
    scheduler.allowCoreThreadTimeOut(true);
    return scheduler;
}

From source file:org.mc4j.ems.impl.jmx.connection.PooledConnectionTracker.java

protected void initTracker() {
    // TODO GH: Build registration system
    RefreshItem connectionRefresh = new ConnectionRefresh(20000, connection);
    refreshItems.add(connectionRefresh);

    RefreshItem attributeRefresh = new MBeanRefresh(20000);
    refreshItems.add(attributeRefresh);//from  w w w. j  ava  2  s .c  om

    // Give names to the threads with a custom thread factory
    executor = new ScheduledThreadPoolExecutor(POOL_SIZE, new ThreadFactory() {
        private AtomicInteger index = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "EMS-ConnectionTracker-" + index.getAndIncrement());
        }
    });

    executor.scheduleAtFixedRate(connectionRefresh, 1000, connectionRefresh.getUpdateDelay(),
            TimeUnit.MILLISECONDS);
    executor.scheduleAtFixedRate(attributeRefresh, 1500, attributeRefresh.getUpdateDelay(),
            TimeUnit.MILLISECONDS);

}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static ScheduledThreadPoolExecutor createScheduledThreadPoolExecutor(final int poolSz,
        final String threadName) {
    return new ScheduledThreadPoolExecutor(poolSz, new ThreadFactory() {
        private AtomicInteger threadNum = new AtomicInteger(0);

        @Override//  w w w  .ja v a  2s  . c  om
        public Thread newThread(Runnable r) {
            if (poolSz == 1)
                return new Thread(r, threadName);
            else
                return new Thread(r, threadName + threadNum.incrementAndGet());
        }
    });
}