本文整理汇总了Java中java.util.concurrent.ScheduledThreadPoolExecutor.scheduleWithFixedDelay方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledThreadPoolExecutor.scheduleWithFixedDelay方法的具体用法?Java ScheduledThreadPoolExecutor.scheduleWithFixedDelay怎么用?Java ScheduledThreadPoolExecutor.scheduleWithFixedDelay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ScheduledThreadPoolExecutor
的用法示例。
在下文中一共展示了ScheduledThreadPoolExecutor.scheduleWithFixedDelay方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSchedule5
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
* scheduleWithFixedDelay executes runnable after given initial delay
*/
public void testSchedule5() throws Exception {
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
try (PoolCleaner cleaner = cleaner(p)) {
final long startTime = System.nanoTime();
final CountDownLatch done = new CountDownLatch(1);
Runnable task = new CheckedRunnable() {
public void realRun() {
done.countDown();
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
}};
ScheduledFuture f =
p.scheduleWithFixedDelay(task, timeoutMillis(),
LONG_DELAY_MS, MILLISECONDS);
await(done);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
f.cancel(true);
}
}
示例2: testScheduleWithFixedDelay_overflow
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
* A fixed delay task with overflowing period should not prevent a
* one-shot task from executing.
* https://bugs.openjdk.java.net/browse/JDK-8051859
*/
@SuppressWarnings("FutureReturnValueIgnored")
public void testScheduleWithFixedDelay_overflow() throws Exception {
final CountDownLatch delayedDone = new CountDownLatch(1);
final CountDownLatch immediateDone = new CountDownLatch(1);
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
try (PoolCleaner cleaner = cleaner(p)) {
final Runnable delayed = () -> {
delayedDone.countDown();
p.submit(() -> immediateDone.countDown());
};
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS);
await(delayedDone);
await(immediateDone);
}
}
示例3: scheduleNow
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void scheduleNow(ScheduledThreadPoolExecutor pool,
Runnable r, int how) {
switch (how) {
case 0:
pool.schedule(r, 0, MILLISECONDS);
break;
case 1:
pool.schedule(Executors.callable(r), 0, DAYS);
break;
case 2:
pool.scheduleWithFixedDelay(r, 0, 1000, NANOSECONDS);
break;
case 3:
pool.scheduleAtFixedRate(r, 0, 1000, MILLISECONDS);
break;
default:
fail(String.valueOf(how));
}
}
示例4: scheduleAtTheEndOfTime
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void scheduleAtTheEndOfTime(ScheduledThreadPoolExecutor pool,
Runnable r, int how) {
switch (how) {
case 0:
pool.schedule(r, Long.MAX_VALUE, MILLISECONDS);
break;
case 1:
pool.schedule(Executors.callable(r), Long.MAX_VALUE, DAYS);
break;
case 2:
pool.scheduleWithFixedDelay(r, Long.MAX_VALUE, 1000, NANOSECONDS);
break;
case 3:
pool.scheduleAtFixedRate(r, Long.MAX_VALUE, 1000, MILLISECONDS);
break;
default:
fail(String.valueOf(how));
}
}
示例5: testExceptionHandler
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
* testExceptionHandler
*/
public void testExceptionHandler() throws Exception {
final CountDownLatch latch = new CountDownLatch(5);
EventObservableExceptionHandler handler = new EventObservableExceptionHandler();
EventObserver<Pair<Thread, Throwable>> observer = new EventObserver<Pair<Thread, Throwable>>() {
@Override
public void update(EventObservable<Pair<Thread, Throwable>> o, Pair<Thread, Throwable> arg) {
Thread thread = arg.getFirst();
assertNotNull(thread);
Throwable error = arg.getSecond();
assertNotNull(error);
System.err.printf("[%02d] Got Error: %s / %s\n",
latch.getCount(), thread.getName(), error);
latch.countDown();
}
};
handler.addObserver(observer);
Runnable r = new ExceptionHandlingRunnable() {
@Override
public void runImpl() {
System.err.println("Executing failing thread!");
throw new RuntimeException("Old and busted!");
}
};
int poolSize = 1;
int stackSize = 1024*128;
ScheduledThreadPoolExecutor executor = ThreadUtil.getScheduledThreadPoolExecutor("TEST", handler, poolSize, stackSize);
executor.scheduleWithFixedDelay(r, 1, 1, TimeUnit.SECONDS);
boolean ret = latch.await(10, TimeUnit.SECONDS);
assertTrue(ret);
}
示例6: serviceStart
import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
@Override
public void serviceStart() throws Exception {
if (planFollower != null) {
scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
scheduledExecutorService.scheduleWithFixedDelay(planFollower, 0L,
planStepSize, TimeUnit.MILLISECONDS);
}
super.serviceStart();
}