Package java.util.concurrent

Examples of java.util.concurrent.ScheduledExecutorService


  }

  private ScheduledExecutorService provideScheduledThreadPoolExecutor(
      Provider<ScheduledRequestScopeExecutor> executorProvider, int threadCount, String name) {
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(name).build();
    ScheduledExecutorService executor;
    if (threadCount == 1) {
      executor = Executors.newSingleThreadScheduledExecutor(threadFactory);
    } else {
      executor = Executors.newScheduledThreadPool(threadCount, threadFactory);
    }
View Full Code Here


  /**
   * Instantiates the ScheduledExecutorService which will iterate through all monitored Metrics at the specified period,
   * send the current value of the Metric, and then clear the value.
   */
  private void setMonitor() {
    ScheduledExecutorService eScheduledService = Executors.newSingleThreadScheduledExecutor();
    eScheduledService.scheduleAtFixedRate(new TimerTask() {
      public void run() {
        updateCount++;
        for (Metricable gmetric : gmetrics) {
          if (updateCount > initPeriod) {
            sendGMetricInit(gmetric);
View Full Code Here

  @Test
  public void testTaskWithoutExecutor() throws InterruptedException
  {
    final int numCores = Runtime.getRuntime().availableProcessors();
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(numCores + 1);
    final Engine engine = new EngineBuilder()
        .setTaskExecutor(scheduler)
        .setTimerScheduler(scheduler)
        .build();

    try
    {
      final Task<Integer> task = new AsyncCallableTask<Integer>(new Callable<Integer>()
      {
        @Override
        public Integer call() throws Exception
        {
          return 1;
        }
      });
      engine.run(task);

      assertTrue(task.await(5, TimeUnit.SECONDS));

      assertTrue(task.isFailed());
      assertTrue(task.getError() instanceof IllegalStateException);
    }
    finally
    {
      engine.shutdown();
      engine.awaitTermination(1, TimeUnit.SECONDS);
      scheduler.shutdownNow();
    }
  }
View Full Code Here

    // instead of using the default executor set up for test.
    final ExecutorService executorService = new ThreadPoolExecutor(1, 1,
                                                                   0, TimeUnit.SECONDS,
                                                                   new ArrayBlockingQueue<Runnable>(1),
                                                                   new ThreadPoolExecutor.AbortPolicy());
    final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    try
    {
      final Engine engine = new EngineBuilder()
          .setTaskExecutor(executorService)
          .setTimerScheduler(scheduledExecutorService)
          .build();

      // First we submit two tasks that will never finish. This saturates the
      // underlying executor by using its only thread and saturating its
      // single slot queue.
      engine.run(neverEndingBlockingTask());
      engine.run(neverEndingBlockingTask());

      // Now we submit another task. The execution loop for this task will fail
      // during submit to the underlying executor. We expect that it will be
      // cancelled.
      final Task<?> task = neverEndingBlockingTask();
      withDisabledLogging(new Runnable()
      {
        @Override
        public void run()
        {
          engine.run(task);
          try
          {
            assertTrue(task.await(5, TimeUnit.SECONDS));
          }
          catch (InterruptedException e)
          {
            // Ignore.
          }
        }
      });
      assertTrue(task.isFailed());
      assertTrue("Expected underlying exception to be instance of RejectedExecutionException, but was: " + task.getError().getCause(),
                 task.getError().getCause() instanceof RejectedExecutionException);

      engine.shutdown();
    }
    finally
    {
      scheduledExecutorService.shutdownNow();
      executorService.shutdownNow();
    }
  }
View Full Code Here

        t.start();

        final int FACTS_PER_POLL = 1000;
        final int POLL_INTERVAL = 500;

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(
                new Runnable() {

                    public void run() {
                        for ( int j = 0; j < FACTS_PER_POLL; j++ ) {
                            ksession.insert( new MyFact() );
                        }
                    }
                },
                0,
                POLL_INTERVAL,
                TimeUnit.MILLISECONDS );


        Thread.sleep( 10200 );

        executor.shutdownNow();
        ksession.halt();
        t.join();

        if (t.getError() != null) {
            fail(t.getError().getMessage());
View Full Code Here

    }

    final ScheduledExecutorService getExitingScheduledExecutorService(
        ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) {
      useDaemonThreadFactory(executor);
      ScheduledExecutorService service = Executors.unconfigurableScheduledExecutorService(executor);
      addDelayedShutdownHook(service, terminationTimeout, timeUnit);
      return service;
    }
View Full Code Here

   * Also, the pool will be {@linkplain ScheduledExecutorService#shutdown() shut down} when the
   * service {@linkplain Service.State#TERMINATED terminates} or
   * {@linkplain Service.State#TERMINATED fails}.
   */
  protected ScheduledExecutorService executor() {
    final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
        new ThreadFactory() {
          @Override public Thread newThread(Runnable runnable) {
            return MoreExecutors.newThread(serviceName(), runnable);
          }
        });
    // Add a listener to shutdown the executor after the service is stopped.  This ensures that the
    // JVM shutdown will not be prevented from exiting after this service has stopped or failed.
    // Technically this listener is added after start() was called so it is a little gross, but it
    // is called within doStart() so we know that the service cannot terminate or fail concurrently
    // with adding this listener so it is impossible to miss an event that we are interested in.
    addListener(new Listener() {
      @Override public void terminated(State from) {
        executor.shutdown();
      }
      @Override public void failed(State from, Throwable failure) {
        executor.shutdown();
      }}, MoreExecutors.sameThreadExecutor());
    return executor;
  }
View Full Code Here

        if (oobExecutor != null) {
            if (!(transport.getOOBThreadPool() instanceof ManagedExecutorService)) {
                transport.setOOBThreadPool(new ManagedExecutorService(oobExecutor));
            }
        }
        ScheduledExecutorService timerExecutor = transportConfig.getTimerExecutor();
        if (timerExecutor != null) {
            if (!(transport.getTimer() instanceof TimerSchedulerAdapter)) {
                this.setValue(transport, "timer", new TimerSchedulerAdapter(new ManagedScheduledExecutorService(timerExecutor)));
            }
        }
View Full Code Here

          }
        });
      }
    }

    /**
     * Due to the lack of contract in CDI guaranteeing when beans will be available, we use an executor to search for
     * the beans every 100ms until it finds them. Or, after a 25 seconds, blow up if they don't become available.
     */
    final ScheduledExecutorService startupScheduler = Executors.newScheduledThreadPool(1);
    startupScheduler.scheduleAtFixedRate(
        new StartupCallback(beanManager, bus, startupScheduler, 25), 0, 100, TimeUnit.MILLISECONDS);

    for (final Class<?> remoteInterfaceType : managedTypes.getRemoteInterfaces()) {
      createRPCScaffolding(remoteInterfaceType, bus, beanManager);
    }
View Full Code Here

         PageCursorsInfo cursorACKs = PrintPages.loadCursorACKs(arg[1]);

         Set<Long> pgTXs = cursorACKs.getPgTXs();

         ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
         final ExecutorService executor = Executors.newFixedThreadPool(10);
         ExecutorFactory execfactory = new ExecutorFactory()
         {

            public Executor getExecutor()
View Full Code Here

TOP

Related Classes of java.util.concurrent.ScheduledExecutorService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.