Example usage for java.util.concurrent Executors newScheduledThreadPool

List of usage examples for java.util.concurrent Executors newScheduledThreadPool

Introduction

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

Prototype

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 

Source Link

Document

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.

Usage

From source file:io.smartspaces.workbench.devinfrastructure.StandaloneDevelopmentInfrastructure.java

@Override
public void startup() {
    log = new Jdk14Logger("smartspaces-Development-Infrastructure");
    try {/*from  w w w  .  j a v a 2s  .  c om*/
        executorService = Executors.newScheduledThreadPool(NUMBER_THREADS_IN_POOL);

        initializeRosEnvironment();
        initializeRosMasterController();

    } catch (Throwable e) {
        throw SmartSpacesException.newFormattedException(e,
                "Could not start up the Smart Spaces Development Infrastructure");
    }
}

From source file:org.wso2.carbon.identity.mgt.store.RegistryCleanUpService.java

/**
 * @param initialDelay/*from  w  ww  . j  a  v  a2  s.  co  m*/
 * @param delayBetweenRuns
 */
public RegistryCleanUpService(long initialDelay, long delayBetweenRuns) {
    this.initialDelay = initialDelay;
    this.delayBetweenRuns = delayBetweenRuns;
    this.scheduler = Executors.newScheduledThreadPool(NUM_THREADS);
}

From source file:org.apache.stratos.common.threading.StratosThreadPool.java

/**
 * Returns a scheduled executor for given thread pool size.
 *
 * @param identifier     Thread pool identifier name
 * @param threadPoolSize Thread pool size
 * @return//from ww  w  . j  ava  2s . co  m
 */
public static ScheduledExecutorService getScheduledExecutorService(String identifier, int threadPoolSize) {
    ScheduledExecutorService scheduledExecutorService = scheduledServiceMap.get(identifier);
    if (scheduledExecutorService == null) {
        synchronized (scheduledServiceMapLock) {
            if (scheduledExecutorService == null) {
                scheduledExecutorService = Executors.newScheduledThreadPool(threadPoolSize);
                scheduledServiceMap.put(identifier, scheduledExecutorService);
                log.info(String.format(
                        "Thread pool created: [type] Scheduled Executor Service [id] %s [size] %d", identifier,
                        threadPoolSize));
            }
        }

    }
    return scheduledExecutorService;
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.StandaloneExample.java

/**
 * Runs a number of threads which generate metrics.
 *//*  w w w. ja v a2  s .com*/
public static void generateMetrics(final MetricRegistry metrics, final long metricsToGenerate, final int period,
        final TimeUnit periodTimeUnit, HadoopMetrics2Reporter metrics2Reporter, int numThreads)
        throws Exception {
    final ScheduledExecutorService pool = Executors.newScheduledThreadPool(numThreads);
    final CountDownLatch latch = new CountDownLatch(numThreads);

    for (int i = 0; i < numThreads; i++) {
        final int id = i;
        final int halfPeriod = (period / 2);
        Runnable task = new Runnable() {
            private long executions = 0;
            final Random r = new Random();

            @Override
            public void run() {
                if (executions >= metricsToGenerate) {
                    return;
                }
                metrics.counter("foo counter thread" + id).inc();
                executions++;
                if (executions < metricsToGenerate) {
                    pool.schedule(this, period + r.nextInt(halfPeriod), periodTimeUnit);
                } else {
                    latch.countDown();
                }
            }
        };
        pool.schedule(task, period, periodTimeUnit);
    }

    while (!latch.await(2, TimeUnit.SECONDS)) {
        metrics2Reporter.printQueueDebugMessage();
    }

    pool.shutdown();
    pool.awaitTermination(5000, TimeUnit.SECONDS);
}

From source file:org.kaaproject.kaa.client.channel.failover.DefaultFailoverManagerTest.java

@Before
public void setUp() {
    channelManager = Mockito.mock(KaaChannelManager.class);
    context = Mockito.mock(ExecutorContext.class);
    Mockito.when(context.getScheduledExecutor()).thenReturn(Executors.newScheduledThreadPool(1));
    FailoverStrategy failoverStrategy = new DefaultFailoverStrategy(BOOTSTRAP_RETRY_PERIOD, 1, 1,
            TimeUnit.MILLISECONDS);
    failoverManager = new DefaultFailoverManager(channelManager, context, failoverStrategy,
            RESOLUTION_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    resolutionProgressMap = Mockito//from w  w  w  .ja  va2 s  .  com
            .spy(new HashMap<ServerType, DefaultFailoverManager.AccessPointIdResolution>());
    ReflectionTestUtils.setField(failoverManager, "resolutionProgressMap", resolutionProgressMap);
}

From source file:com.sishuok.bigpipe.handler.BigpipeTaskReturnValueHandler.java

@Override
public void afterPropertiesSet() throws Exception {
    if (executor == null) {
        executor = Executors.newScheduledThreadPool(10);
    }/* ww w  . j  a  va 2 s .co  m*/
    if (pageletFinder == null) {
        pageletFinder = applicationContext.getBean(PageletFinder.class);
    }
    if (pageletViewResolver == null) {
        pageletViewResolver = applicationContext.getBean(PageletViewResolver.class);
    }

    Assert.notNull(pageletFinder, "must define a PageletFinder");
    Assert.notNull(pageletViewResolver, "must define a PageletViewResolver");
}

From source file:interactivespaces.util.web.WebSocketTest.java

@Before
public void setup() {
    log = new Jdk14Logger("goober"); // Mockito.mock(Log.class);

    threadPool = Executors.newScheduledThreadPool(100);
}

From source file:org.openspaces.example.data.feeder.BroadcastDataCounter.java

@PostConstruct
public void construct() throws Exception {
    Assert.notNull(dataProcessor, "dataProcessor proeprty must be set");
    System.out.println("--- STARTING BROADCAST REMOTING COUNTER WITH CYCLE [" + defaultDelay + "]");
    viewCounterTask = new ViewCounterTask();
    executorService = Executors.newScheduledThreadPool(1);
    sf = executorService.scheduleAtFixedRate(viewCounterTask, defaultDelay, defaultDelay,
            TimeUnit.MILLISECONDS);
}

From source file:org.ebayopensource.scc.track.TrackerClientTest.java

@Test
public void testInit() {
    AppConfiguration config = mock(AppConfiguration.class);
    when(config.getString("trackerClient.endpoint.host")).thenReturn("127.0.0.1");
    when(config.getInt("trackerClient.endpoint.port")).thenReturn(8080);
    when(config.getString("trackerClient.endpoint.path"))
            .thenReturn("/build-service/webapi/serviceCache/tracker");
    when(config.getBoolean("trackerClient.endpoint.isSecure")).thenReturn(false);

    TrackerClient tc = new TrackerClient(config, Executors.newScheduledThreadPool(1));
    HttpHost httpHost = tc.createHttpHost();
    assertEquals(8080, httpHost.getPort());
    assertEquals("http", httpHost.getSchemeName());

    when(config.getInt("trackerClient.endpoint.port")).thenReturn(null);
    tc = new TrackerClient(config, Executors.newScheduledThreadPool(1));
    httpHost = tc.createHttpHost();//from   w  ww  .  j  a  v a  2 s  .co  m
    assertEquals(80, httpHost.getPort());

    when(config.getBoolean("trackerClient.endpoint.isSecure")).thenReturn(true);
    tc = new TrackerClient(config, Executors.newScheduledThreadPool(1));
    httpHost = tc.createHttpHost();
    assertEquals(443, httpHost.getPort());

    when(config.getBoolean("trackerClient.endpoint.isSecure")).thenReturn(true);
    when(config.getInt("trackerClient.endpoint.port")).thenReturn(96542);
    tc = new TrackerClient(config, Executors.newScheduledThreadPool(1));
    httpHost = tc.createHttpHost();
    assertEquals(96542, httpHost.getPort());
}

From source file:interactivespaces.service.comm.network.NettyUdpSocketTest.java

@Before
public void setup() {
    log = new Jdk14Logger("goober");

    threadPool = Executors.newScheduledThreadPool(100);

    SimpleInteractiveSpacesEnvironment spaceEnvironment = new SimpleInteractiveSpacesEnvironment();
    spaceEnvironment.setExecutorService(threadPool);

    clientService = new NettyUdpClientNetworkCommunicationEndpointService();
    clientService.setSpaceEnvironment(spaceEnvironment);
    clientService.startup();/*from   w ww  . j ava 2s  .c o m*/

    serverService = new NettyUdpServerNetworkCommunicationEndpointService();
    serverService.setSpaceEnvironment(spaceEnvironment);
    serverService.startup();
}