本文整理汇总了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);
}
示例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();
}
示例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);
}
示例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();
}
示例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());
}
示例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());
}
}
示例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);
}
示例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();
}
}
示例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();
}
示例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
}
示例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());
}
示例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();
}
示例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();
}
}
示例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);
}
示例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();
}
}