当前位置: 首页>>代码示例>>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;未经允许,请勿转载。