當前位置: 首頁>>代碼示例>>Java>>正文


Java ScheduledThreadPoolExecutor.scheduleWithFixedDelay方法代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:ScheduledExecutorTest.java

示例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);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:ScheduledExecutorTest.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:DelayOverflow.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:DelayOverflow.java

示例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);
}
 
開發者ID:s-store,項目名稱:sstore-soft,代碼行數:37,代碼來源:TestThreadUtil.java

示例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();
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:10,代碼來源:AbstractReservationSystem.java


注:本文中的java.util.concurrent.ScheduledThreadPoolExecutor.scheduleWithFixedDelay方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。