当前位置: 首页>>代码示例>>Java>>正文


Java ScheduledExecutorService.shutdown方法代码示例

本文整理汇总了Java中java.util.concurrent.ScheduledExecutorService.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledExecutorService.shutdown方法的具体用法?Java ScheduledExecutorService.shutdown怎么用?Java ScheduledExecutorService.shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ScheduledExecutorService的用法示例。


在下文中一共展示了ScheduledExecutorService.shutdown方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDelay

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testDelay() throws InterruptedException {
    ScheduledExecutorService ses =
        Executors.newSingleThreadScheduledExecutor();

    SingletonTask st1 = new SingletonTask(ses, new Runnable() {
        @Override
        public void run() {
            ran += 1;
            time = System.nanoTime();
        }
    });
    st1.reschedule(10, TimeUnit.MILLISECONDS);
    assertFalse("Check that task hasn't run yet", ran > 0);

    ses.shutdown();
    ses.awaitTermination(5, TimeUnit.SECONDS);

    assertEquals("Check that task ran", 1, ran);
}
 
开发者ID:xuraylei,项目名称:fresco_floodlight,代码行数:21,代码来源:SingletonTaskTest.java

示例2: realMain

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public static void realMain(String... args) throws InterruptedException {
    // our tickle service
    ScheduledExecutorService tickleService =
        new ScheduledThreadPoolExecutor(concurrency) {
            // We override decorateTask() to return a custom
            // RunnableScheduledFuture which explicitly removes
            // itself from the queue after cancellation.
            protected <V> RunnableScheduledFuture<V>
                decorateTask(Runnable runnable,
                             RunnableScheduledFuture<V> task) {
                final ScheduledThreadPoolExecutor exec = this;
                return new CustomRunnableScheduledFuture<V>(task) {
                    // delegate to wrapped task, except for:
                    public boolean cancel(boolean b) {
                        // cancel wrapped task & remove myself from the queue
                        return (task().cancel(b)
                                && exec.remove(this));}};}};

    for (int i = 0; i < concurrency; i++)
        new ScheduledTickle(i, tickleService)
            .setUpdateInterval(25, MILLISECONDS);

    done.await();
    tickleService.shutdown();
    pass();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:ScheduledTickleService.java

示例3: testBasic

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testBasic() throws InterruptedException {
    ScheduledExecutorService ses =
        Executors.newSingleThreadScheduledExecutor();

    SingletonTask st1 = new SingletonTask(ses, new Runnable() {
        @Override
        public void run() {
            ran += 1;
        }
    });
    st1.reschedule(0, null);
    ses.shutdown();
    ses.awaitTermination(5, TimeUnit.SECONDS);

    assertEquals("Check that task ran", 1, ran);
}
 
开发者ID:xuraylei,项目名称:fresco_floodlight,代码行数:18,代码来源:SingletonTaskTest.java

示例4: start

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private void start() throws Exception {
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    startClient(executorService);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("ru.otus:type=Server");
    MirrorServer server = new MirrorServer();
    mbs.registerMBean(server, name);

    server.start();

    executorService.shutdown();
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_04,代码行数:14,代码来源:ServerMain.java

示例5: testReschedule

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
 * Test to make sure we reschedule the task for execution if it has already in progress.
 */
@Test
public void testReschedule() throws Exception {
  ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
  OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex);

  final CountDownLatch taskRunning = new CountDownLatch(1);
  final CountDownLatch continueTask = new CountDownLatch(1);
  final AtomicInteger counter = new AtomicInteger();

  Callable waitForLatch = new Callable() {

    public Object call() throws Exception {
      taskRunning.countDown();
      continueTask.await();
      counter.incrementAndGet();
      return null;
    }
  };

  Runnable increment = new Runnable() {

    public void run() {
      counter.incrementAndGet();
    }
  };

  decorator.schedule(waitForLatch, 0, TimeUnit.SECONDS);
  taskRunning.await();
  decorator.schedule(increment, 0, TimeUnit.SECONDS);

  assertEquals(0, counter.get());
  continueTask.countDown();

  ex.shutdown();
  ex.awaitTermination(60, TimeUnit.SECONDS);
  assertEquals(2, counter.get());
}
 
开发者ID:ampool,项目名称:monarch,代码行数:41,代码来源:OneTaskOnlyDecoratorJUnitTest.java

示例6: restartHeartbeatSender

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
 * Restarts heartbeatSender executor.
 */
private void restartHeartbeatSender() {
    try {
        ScheduledExecutorService prevSender = heartBeatSender;
        heartBeatSender = Executors.newSingleThreadScheduledExecutor(
                groupedThreads("onos/cluster/membership", "heartbeat-sender-%d", log));
        heartBeatSender.scheduleWithFixedDelay(this::heartbeat, 0,
                                               heartbeatInterval, TimeUnit.MILLISECONDS);
        prevSender.shutdown();
    } catch (Exception e) {
        log.warn(e.getMessage());
    }
}
 
开发者ID:shlee89,项目名称:athena,代码行数:16,代码来源:DistributedClusterStore.java

示例7: testReschedule

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testReschedule() throws InterruptedException {
    ScheduledExecutorService ses =
        Executors.newSingleThreadScheduledExecutor();

    final Object tc = this;
    SingletonTask st1 = new SingletonTask(ses, new Runnable() {
        @Override
        public void run() {
            synchronized (tc) {
                ran += 1;
            }
            time = System.nanoTime();
        }
    });

    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);
    st1.reschedule(20, TimeUnit.MILLISECONDS);
    Thread.sleep(5);
    assertFalse("Check that task hasn't run yet", ran > 0);

    ses.shutdown();
    ses.awaitTermination(5, TimeUnit.SECONDS);

    assertEquals("Check that task ran only once", 1, ran);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:47,代码来源:SingletonTaskTest.java

示例8: cancellation

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test(expected = RuntimeException.class)
public void cancellation() throws CheckedFutureException {
  final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  try {
    final Future<Long> f = ds.execute("SELECT pg_sleep(999)");
    f.raise(new RuntimeException());
    f.get(timeout);
  } finally {
    scheduler.shutdown();
  }
}
 
开发者ID:traneio,项目名称:ndbc,代码行数:12,代码来源:DataSourceTest.java

示例9: start

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private void start() throws Exception {
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    startClients(CLIENTS_COUNT, executorService);

    //startLogServer();
    startMirrorServer();
    //startBlockingServer();

    executorService.shutdown();
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_04,代码行数:11,代码来源:ServerMain.java

示例10: test2

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void test2 () throws Exception
{
    final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor ();

    final ProcessBuilder pb = new ProcessBuilder ( "sleep", "3" ); // FIXME: works only on unix
    final AbstractScheduledInput input = new ProcessInput ( executor, pb, Charset.forName ( "UTF-8" ), 1000 );

    final TestListener listener = new TestListener ();

    input.addInputListener ( listener );

    logger.debug ( "test2 - start" );

    input.start ();
    Thread.sleep ( 100 );
    logger.debug ( "test2 - stop" );
    input.stop ();
    logger.debug ( "test2 - dispose" );
    input.dispose ();

    logger.debug ( "test2 - shutdown" );
    executor.shutdown ();
    logger.debug ( "test2 - wait" );
    executor.awaitTermination ( Long.MAX_VALUE, TimeUnit.MINUTES );
    logger.debug ( "test2 - done" );

    dumpData ( listener );

    // TODO: test
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:32,代码来源:ProcessTest.java

示例11: testExecuteOnlyOnce

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
 * Test to make sure we only execute the task once no matter how many times we schedule it.
 */
@Test
public void testExecuteOnlyOnce() throws Exception {
  ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);

  MyConflationListener listener = new MyConflationListener();
  OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);

  final CountDownLatch latch = new CountDownLatch(1);
  ex.submit(new Callable() {

    public Object call() throws Exception {
      latch.await();
      return null;
    }
  });

  final AtomicInteger counter = new AtomicInteger();

  Runnable increment = new Runnable() {

    public void run() {
      counter.incrementAndGet();
    }
  };

  for (int i = 0; i < 50; i++) {
    decorator.schedule(increment, 0, TimeUnit.SECONDS);
  }

  assertEquals(0, counter.get());
  latch.countDown();
  ex.shutdown();
  ex.awaitTermination(60, TimeUnit.SECONDS);
  assertEquals(1, counter.get());
  assertEquals(49, listener.getDropCount());
}
 
开发者ID:ampool,项目名称:monarch,代码行数:40,代码来源:OneTaskOnlyDecoratorJUnitTest.java

示例12: start

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private void start() throws Exception {
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    startClient(executorService);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("ru.otus:type=Server");
    MirrorSocketMsgServer server = new MirrorSocketMsgServer();
    mbs.registerMBean(server, name);

    server.start();

    executorService.shutdown();
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_06,代码行数:14,代码来源:ServerMain.java

示例13: trialRunEngine

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
 * Trial run of the engine. Shuts down after {@code timeout} seconds after startup.
 *
 * @param engine the engine.
 * @param timeout timeout in seconds.
 */
public static void trialRunEngine(Engine engine, int timeout) {
    final Semaphore semaphore = new Semaphore(0, true);

    // Startup the engine. After startup the engine runs on the threads other than the current one.
    engine.startup();

    try {
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.schedule(() -> {
            // Release the semaphore after timeout.
            semaphore.release();
        }, timeout, TimeUnit.SECONDS);

        try {
            // Wait for releasing the semaphore after timeout.
            semaphore.acquire();
        } catch (InterruptedException e) {
            logger.warn("trialRunEngine", e);
        }

        executor.shutdown();
    } finally {
        // Shutdown the engine.
        engine.shutdown();
    }
}
 
开发者ID:softelnet,项目名称:sponge,代码行数:33,代码来源:SpongeUtils.java

示例14: fixedRate

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
private static void fixedRate() throws Exception {
  // starts immediately after the task has run
  ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

  Runnable task = () -> {
    System.out.println("Scheduling: " + System.nanoTime() + " " + ZonedDateTime.now());
    try {
      TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("Ready:      " +  System.nanoTime() + " " + ZonedDateTime.now());
  };

  int initialDelay = 0;
  int period = 8;
  executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);

  ScheduledExecutorService watch = Executors.newScheduledThreadPool(1);
  Runnable watcher = () -> {
    if (new File("stop").exists()) {
      System.out.println("Stopping executor because file 'stop' exists.");
      executor.shutdown();
      watch.shutdown();
    }
  };

  watch.scheduleWithFixedDelay(watcher, 1, 1, TimeUnit.SECONDS);
}
 
开发者ID:EHRI,项目名称:rs-aggregator,代码行数:30,代码来源:TestTest.java

示例15: testFix

import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testFix() throws InterruptedException, KafkaCruiseControlException {
  LinkedBlockingDeque<Anomaly> anomalies = new LinkedBlockingDeque<>();
  AnomalyNotifier mockAnomalyNotifier = EasyMock.mock(AnomalyNotifier.class);
  BrokerFailureDetector mockBrokerFailureDetector = EasyMock.createNiceMock(BrokerFailureDetector.class);
  GoalViolationDetector mockGoalViolationDetector = EasyMock.createNiceMock(GoalViolationDetector.class);
  ScheduledExecutorService mockDetectorScheduler = EasyMock.mock(ScheduledExecutorService.class);
  ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
  KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);

  EasyMock.expect(mockAnomalyNotifier.onGoalViolation(EasyMock.isA(GoalViolations.class)))
          .andReturn(AnomalyNotificationResult.fix());

  // Starting periodic goal violation detection.
  EasyMock.expect(mockDetectorScheduler.scheduleAtFixedRate(EasyMock.eq(mockGoalViolationDetector),
                                                            EasyMock.anyLong(),
                                                            EasyMock.eq(3000L),
                                                            EasyMock.eq(TimeUnit.MILLISECONDS)))
          .andReturn(null);

  // Starting anomaly handler
  EasyMock.expect(mockDetectorScheduler.submit(EasyMock.isA(AnomalyDetector.AnomalyHandlerTask.class)))
          .andDelegateTo(executorService);

  mockDetectorScheduler.shutdown();
  EasyMock.expectLastCall().andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.awaitTermination(3000L, TimeUnit.MILLISECONDS)).andDelegateTo(executorService);
  EasyMock.expect(mockDetectorScheduler.isTerminated()).andDelegateTo(executorService);

  // The following state are used to test the delayed check when executor is busy.
  EasyMock.expect(mockKafkaCruiseControl.state())
          .andReturn(new KafkaCruiseControlState(ExecutorState.noTaskInProgress(), null, null));
  EasyMock.expect(mockKafkaCruiseControl.rebalance(Collections.emptyList(), false, null))
          .andReturn(null);

  EasyMock.replay(mockAnomalyNotifier);
  EasyMock.replay(mockBrokerFailureDetector);
  EasyMock.replay(mockGoalViolationDetector);
  EasyMock.replay(mockDetectorScheduler);
  EasyMock.replay(mockKafkaCruiseControl);

  AnomalyDetector anomalyDetector = new AnomalyDetector(anomalies, 3000L, mockKafkaCruiseControl, mockAnomalyNotifier,
                                                        mockGoalViolationDetector, mockBrokerFailureDetector,
                                                        mockDetectorScheduler);

  try {
    anomalyDetector.startDetection();
    anomalies.add(new GoalViolations());
    while (!anomalies.isEmpty()) {
      // Just wait for the anomalies to be drained.
    }
    anomalyDetector.shutdown();
    assertTrue(executorService.awaitTermination(5000, TimeUnit.MILLISECONDS));
    EasyMock.verify(mockAnomalyNotifier, mockDetectorScheduler, mockKafkaCruiseControl);
  } finally {
    executorService.shutdown();
  }
}
 
开发者ID:linkedin,项目名称:cruise-control,代码行数:59,代码来源:AnomalyDetectorTest.java


注:本文中的java.util.concurrent.ScheduledExecutorService.shutdown方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。