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