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