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


Java ScheduledThreadPoolExecutor.execute方法代碼示例

本文整理匯總了Java中java.util.concurrent.ScheduledThreadPoolExecutor.execute方法的典型用法代碼示例。如果您正苦於以下問題:Java ScheduledThreadPoolExecutor.execute方法的具體用法?Java ScheduledThreadPoolExecutor.execute怎麽用?Java ScheduledThreadPoolExecutor.execute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.ScheduledThreadPoolExecutor的用法示例。


在下文中一共展示了ScheduledThreadPoolExecutor.execute方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: ServerConnector

import java.util.concurrent.ScheduledThreadPoolExecutor; //導入方法依賴的package包/類
public ServerConnector(int ownPort, String host, int port) {
    if (ownPort == port) {
        System.out.println("Cannot connect to own server!");
        return;
    }

    server = new EchoServer(ownPort);
    client = new EchoClient(host, port);

    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);

    executor.execute(() -> {
        try {
            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
 
開發者ID:AlphaHelixDev,項目名稱:AlphaLibary,代碼行數:20,代碼來源:ServerConnector.java

示例2: testGetActiveCount

import java.util.concurrent.ScheduledThreadPoolExecutor; //導入方法依賴的package包/類
/**
 * getActiveCount increases but doesn't overestimate, when a
 * thread becomes active
 */
public void testGetActiveCount() throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        assertEquals(0, p.getActiveCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(1, p.getActiveCount());
                await(done);
            }});
        await(threadStarted);
        assertEquals(1, p.getActiveCount());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:ScheduledExecutorTest.java

示例3: testShutdownThreadPool

import java.util.concurrent.ScheduledThreadPoolExecutor; //導入方法依賴的package包/類
@Test
public void testShutdownThreadPool() throws InterruptedException {
  ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
  executor.execute(sampleRunnable);
  boolean ret = ShutdownThreadsHelper.shutdownExecutorService(executor);
  boolean isTerminated = executor.isTerminated();
  assertEquals("Incorrect return value", ret, isTerminated);
  assertTrue("ExecutorService is not shutdown", isTerminated);
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:10,代碼來源:TestShutdownThreadsHelper.java

示例4: testExecute

import java.util.concurrent.ScheduledThreadPoolExecutor; //導入方法依賴的package包/類
/**
 * execute successfully executes a runnable
 */
public void testExecute() throws InterruptedException {
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch done = new CountDownLatch(1);
        final Runnable task = new CheckedRunnable() {
            public void realRun() { done.countDown(); }};
        p.execute(task);
        await(done);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:ScheduledExecutorTest.java

示例5: testShutdownNow

import java.util.concurrent.ScheduledThreadPoolExecutor; //導入方法依賴的package包/類
/**
 * shutdownNow returns a list containing tasks that were not run,
 * and those tasks are drained from the queue
 */
public void testShutdownNow() throws InterruptedException {
    final int poolSize = 2;
    final int count = 5;
    final AtomicInteger ran = new AtomicInteger(0);
    final ScheduledThreadPoolExecutor p =
        new ScheduledThreadPoolExecutor(poolSize);
    final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
    Runnable waiter = new CheckedRunnable() { public void realRun() {
        threadsStarted.countDown();
        try {
            MILLISECONDS.sleep(2 * LONG_DELAY_MS);
        } catch (InterruptedException success) {}
        ran.getAndIncrement();
    }};
    for (int i = 0; i < count; i++)
        p.execute(waiter);
    await(threadsStarted);
    assertEquals(poolSize, p.getActiveCount());
    assertEquals(0, p.getCompletedTaskCount());
    final List<Runnable> queuedTasks;
    try {
        queuedTasks = p.shutdownNow();
    } catch (SecurityException ok) {
        return; // Allowed in case test doesn't have privs
    }
    assertTrue(p.isShutdown());
    assertTrue(p.getQueue().isEmpty());
    assertEquals(count - poolSize, queuedTasks.size());
    assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    assertTrue(p.isTerminated());
    assertEquals(poolSize, ran.get());
    assertEquals(poolSize, p.getCompletedTaskCount());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:38,代碼來源:ScheduledExecutorTest.java


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