当前位置: 首页>>代码示例>>Java>>正文


Java ScheduledThreadPoolExecutor.schedule方法代码示例

本文整理汇总了Java中java.util.concurrent.ScheduledThreadPoolExecutor.schedule方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledThreadPoolExecutor.schedule方法的具体用法?Java ScheduledThreadPoolExecutor.schedule怎么用?Java ScheduledThreadPoolExecutor.schedule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ScheduledThreadPoolExecutor的用法示例。


在下文中一共展示了ScheduledThreadPoolExecutor.schedule方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void test(String[] args) throws Throwable {

        ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(0);
        Runnable task = new Runnable() {
            public void run() {
                taskRun = true;
            }
        };
        check(pool.getCorePoolSize() == 0);

        pool.schedule(task, 1, TimeUnit.SECONDS);

        pool.shutdown();
        check(pool.awaitTermination(20L, TimeUnit.SECONDS));
        check(pool.getCorePoolSize() == 0);
        check(taskRun);
    }
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:ZeroCorePoolSize.java

示例2: testGetQueue

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * getQueue returns the work queue, which contains queued tasks
 */
public void testGetQueue() throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        ScheduledFuture[] tasks = new ScheduledFuture[5];
        for (int i = 0; i < tasks.length; i++) {
            Runnable r = new CheckedRunnable() {
                public void realRun() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                }};
            tasks[i] = p.schedule(r, 1, MILLISECONDS);
        }
        await(threadStarted);
        BlockingQueue<Runnable> q = p.getQueue();
        assertTrue(q.contains(tasks[tasks.length - 1]));
        assertFalse(q.contains(tasks[0]));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:ScheduledExecutorSubclassTest.java

示例3: testSchedule1

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * delayed schedule of callable successfully executes after delay
 */
public void testSchedule1() throws Exception {
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p)) {
        final long startTime = System.nanoTime();
        final CountDownLatch done = new CountDownLatch(1);
        Callable task = new CheckedCallable<Boolean>() {
            public Boolean realCall() {
                done.countDown();
                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
                return Boolean.TRUE;
            }};
        Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
        assertSame(Boolean.TRUE, f.get());
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
        assertEquals(0L, done.getCount());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:ScheduledExecutorTest.java

示例4: testSchedule3

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * delayed schedule of runnable successfully executes after delay
 */
public void testSchedule3() 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());
            }};
        Future f = p.schedule(task, timeoutMillis(), MILLISECONDS);
        await(done);
        assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ScheduledExecutorTest.java

示例5: testPurge

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * purge eventually removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final ScheduledFuture[] tasks = new ScheduledFuture[5];
    final Runnable releaser = new Runnable() { public void run() {
        for (ScheduledFuture task : tasks)
            if (task != null) task.cancel(true); }};
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p, releaser)) {
        for (int i = 0; i < tasks.length; i++)
            tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(),
                                  LONG_DELAY_MS, MILLISECONDS);
        int max = tasks.length;
        if (tasks[4].cancel(true)) --max;
        if (tasks[3].cancel(true)) --max;
        // There must eventually be an interference-free point at
        // which purge will not fail. (At worst, when queue is empty.)
        long startTime = System.nanoTime();
        do {
            p.purge();
            long count = p.getTaskCount();
            if (count == max)
                return;
        } while (millisElapsedSince(startTime) < LONG_DELAY_MS);
        fail("Purge failed to remove cancelled tasks");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:ScheduledExecutorTest.java

示例6: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
private static void test(boolean allowTimeout) throws Exception {
    CountingThreadFactory ctf = new CountingThreadFactory();
    ScheduledThreadPoolExecutor stpe
        = new ScheduledThreadPoolExecutor(10, ctf);
    try {
        // schedule a dummy task in the "far future"
        Runnable nop = new Runnable() { public void run() {}};
        stpe.schedule(nop, FAR_FUTURE_MS, MILLISECONDS);
        stpe.setKeepAliveTime(1L, MILLISECONDS);
        stpe.allowCoreThreadTimeOut(allowTimeout);
        MILLISECONDS.sleep(12L);
    } finally {
        stpe.shutdownNow();
        if (!stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
            throw new AssertionError("timed out");
    }
    if (ctf.count.get() > 1)
        throw new AssertionError(
            String.format("%d threads created, 1 expected",
                          ctf.count.get()));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:ThreadRestarts.java

示例7: 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

示例8: 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

示例9: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void test(String[] args) throws Throwable {

        ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(0);
        Runnable task = new Runnable() {
            public void run() {
                taskRun = true;
            }
        };
        check(pool.getCorePoolSize() == 0);

        pool.schedule(task, 12L, MILLISECONDS);

        pool.shutdown();
        check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
        check(pool.getCorePoolSize() == 0);
        check(taskRun);
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:ZeroCorePoolSize.java

示例10: test

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
void test(ScheduledThreadPoolExecutor p) throws Throwable {
    Runnable dummy = new Runnable() { public void run() {
        throw new AssertionError("shouldn't get here"); }};
    BlockingQueue q = p.getQueue();
    ReentrantLock lock = getField(q, "lock");
    Condition available = getField(q, "available");

    equal(0, p.getPoolSize());
    equal(0, p.getLargestPoolSize());
    equal(0L, p.getTaskCount());
    equal(0L, p.getCompletedTaskCount());
    p.schedule(dummy, 1L, HOURS);
    // Ensure one pool thread actually waits in timed queue poll
    awaitHasWaiters(lock, available, LONG_DELAY_MS);
    equal(1, p.getPoolSize());
    equal(1, p.getLargestPoolSize());
    equal(1L, p.getTaskCount());
    equal(0L, p.getCompletedTaskCount());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ZeroCoreThreads.java

示例11: main

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
public static void main(String[] args)
{
    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(
            10);
    Runnable event = new Runnable()
    {

        @Override
        public void run()
        {
            System.out.println("吃饭,睡觉,打豆豆");

        }
    };
    scheduler.schedule(event, 1, TimeUnit.SECONDS);
    scheduler.scheduleAtFixedRate(event, 5, 1, TimeUnit.SECONDS);
}
 
开发者ID:baohongfei,项目名称:think-in-java,代码行数:18,代码来源:SimpleUsage.java

示例12: ping

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
public void ping(Consumer<ServerResult> callback) {
    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);

    exec.schedule(() -> {
        try {
            Socket s = new Socket(server, port);

            DataInputStream in = new DataInputStream(s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());

            out.write(0xFE);

            int b;
            StringBuilder str = new StringBuilder();

            while ((b = in.read()) != -1)
                if (b != 0 && b > 16 && b != 255 && b != 23 && b != 24)
                    str.append((char) b);

            String[] data = str.toString().split("§");
            callback.accept(new ServerResult(Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[0]));
        } catch (IOException e) {
            e.printStackTrace();
            callback.accept(null);
        }
    }, 1, TimeUnit.MILLISECONDS);
}
 
开发者ID:AlphaHelixDev,项目名称:AlphaLibary,代码行数:28,代码来源:ServerPinger.java

示例13: scheduledDeletion

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
public void scheduledDeletion(ScheduledThreadPoolExecutor scheduledExecutor, IUser user, int cooldown) {
	ScheduledFuture<LimitedUser> deletionTask = limitedUsersMap.get(user.getLongID()).getDeletionTask();
	if(deletionTask != null) {
		deletionTask.cancel(false);
	}

	deletionTask = scheduledExecutor.schedule(() -> limitedUsersMap.remove(user.getLongID()), cooldown, TimeUnit.MILLISECONDS);
	limitedUsersMap.get(user.getLongID()).setDeletionTask(deletionTask);
}
 
开发者ID:Shadorc,项目名称:Shadbot,代码行数:10,代码来源:LimitedGuild.java

示例14: testRemove

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * remove(task) removes queued task, and fails to remove active task
 */
public void testRemove() throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor p = new CustomExecutor(1);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        ScheduledFuture[] tasks = new ScheduledFuture[5];
        final CountDownLatch threadStarted = new CountDownLatch(1);
        for (int i = 0; i < tasks.length; i++) {
            Runnable r = new CheckedRunnable() {
                public void realRun() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                }};
            tasks[i] = p.schedule(r, 1, MILLISECONDS);
        }
        await(threadStarted);
        BlockingQueue<Runnable> q = p.getQueue();
        assertFalse(p.remove((Runnable)tasks[0]));
        assertTrue(q.contains((Runnable)tasks[4]));
        assertTrue(q.contains((Runnable)tasks[3]));
        assertTrue(p.remove((Runnable)tasks[4]));
        assertFalse(p.remove((Runnable)tasks[4]));
        assertFalse(q.contains((Runnable)tasks[4]));
        assertTrue(q.contains((Runnable)tasks[3]));
        assertTrue(p.remove((Runnable)tasks[3]));
        assertFalse(q.contains((Runnable)tasks[3]));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:31,代码来源:ScheduledExecutorSubclassTest.java

示例15: testGetQueue

import java.util.concurrent.ScheduledThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * getQueue returns the work queue, which contains queued tasks
 */
public void testGetQueue() throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        ScheduledFuture[] tasks = new ScheduledFuture[5];
        for (int i = 0; i < tasks.length; i++) {
            Runnable r = new CheckedRunnable() {
                public void realRun() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                }};
            tasks[i] = p.schedule(r, 1, MILLISECONDS);
        }
        await(threadStarted);
        BlockingQueue<Runnable> q = p.getQueue();
        assertTrue(q.contains(tasks[tasks.length - 1]));
        assertFalse(q.contains(tasks[0]));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:ScheduledExecutorTest.java


注:本文中的java.util.concurrent.ScheduledThreadPoolExecutor.schedule方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。