scheduledexecutortest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,149 行 · 第 1/3 页
JAVA
1,149 行
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.*;
import java.util.*;
public class ScheduledExecutorTest extends JSR166TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(ScheduledExecutorTest.class);
}
/**
* execute successfully executes a runnable
*/
public void testExecute() {
try {
TrackedShortRunnable runnable =new TrackedShortRunnable();
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
p1.execute(runnable);
assertFalse(runnable.done);
Thread.sleep(SHORT_DELAY_MS);
try { p1.shutdown(); } catch(SecurityException ok) { return; }
try {
Thread.sleep(MEDIUM_DELAY_MS);
} catch(InterruptedException e){
unexpectedException();
}
assertTrue(runnable.done);
try { p1.shutdown(); } catch(SecurityException ok) { return; }
joinPool(p1);
}
catch(Exception e){
unexpectedException();
}
}
/**
* delayed schedule of callable successfully executes after delay
*/
public void testSchedule1() {
try {
TrackedCallable callable = new TrackedCallable();
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
assertFalse(callable.done);
Thread.sleep(MEDIUM_DELAY_MS);
assertTrue(callable.done);
assertEquals(Boolean.TRUE, f.get());
try { p1.shutdown(); } catch(SecurityException ok) { return; }
joinPool(p1);
} catch(RejectedExecutionException e){}
catch(Exception e){
e.printStackTrace();
unexpectedException();
}
}
/**
* delayed schedule of runnable successfully executes after delay
*/
public void testSchedule3() {
try {
TrackedShortRunnable runnable = new TrackedShortRunnable();
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
Thread.sleep(SHORT_DELAY_MS);
assertFalse(runnable.done);
Thread.sleep(MEDIUM_DELAY_MS);
assertTrue(runnable.done);
try { p1.shutdown(); } catch(SecurityException ok) { return; }
joinPool(p1);
} catch(Exception e){
unexpectedException();
}
}
/**
* scheduleAtFixedRate executes runnable after given initial delay
*/
public void testSchedule4() {
try {
TrackedShortRunnable runnable = new TrackedShortRunnable();
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
assertFalse(runnable.done);
Thread.sleep(MEDIUM_DELAY_MS);
assertTrue(runnable.done);
h.cancel(true);
joinPool(p1);
} catch(Exception e){
unexpectedException();
}
}
static class RunnableCounter implements Runnable {
AtomicInteger count = new AtomicInteger(0);
public void run() { count.getAndIncrement(); }
}
/**
* scheduleWithFixedDelay executes runnable after given initial delay
*/
public void testSchedule5() {
try {
TrackedShortRunnable runnable = new TrackedShortRunnable();
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
assertFalse(runnable.done);
Thread.sleep(MEDIUM_DELAY_MS);
assertTrue(runnable.done);
h.cancel(true);
joinPool(p1);
} catch(Exception e){
unexpectedException();
}
}
/**
* scheduleAtFixedRate executes series of tasks at given rate
*/
public void testFixedRateSequence() {
try {
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
RunnableCounter counter = new RunnableCounter();
ScheduledFuture h =
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS);
Thread.sleep(SMALL_DELAY_MS);
h.cancel(true);
int c = counter.count.get();
// By time scaling conventions, we must have at least
// an execution per SHORT delay, but no more than one SHORT more
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
joinPool(p1);
} catch(Exception e){
unexpectedException();
}
}
/**
* scheduleWithFixedDelay executes series of tasks with given period
*/
public void testFixedDelaySequence() {
try {
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
RunnableCounter counter = new RunnableCounter();
ScheduledFuture h =
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS);
Thread.sleep(SMALL_DELAY_MS);
h.cancel(true);
int c = counter.count.get();
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
joinPool(p1);
} catch(Exception e){
unexpectedException();
}
}
/**
* execute (null) throws NPE
*/
public void testExecuteNull() {
ScheduledThreadPoolExecutor se = null;
try {
se = new ScheduledThreadPoolExecutor(1);
se.execute(null);
shouldThrow();
} catch(NullPointerException success){}
catch(Exception e){
unexpectedException();
}
joinPool(se);
}
/**
* schedule (null) throws NPE
*/
public void testScheduleNull() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
TrackedCallable callable = null;
Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(NullPointerException success){}
catch(Exception e){
unexpectedException();
}
joinPool(se);
}
/**
* execute throws RejectedExecutionException if shutdown
*/
public void testSchedule1_RejectedExecutionException() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
se.shutdown();
se.schedule(new NoOpRunnable(),
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(RejectedExecutionException success){
} catch (SecurityException ok) {
}
joinPool(se);
}
/**
* schedule throws RejectedExecutionException if shutdown
*/
public void testSchedule2_RejectedExecutionException() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
se.shutdown();
se.schedule(new NoOpCallable(),
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(RejectedExecutionException success){
} catch (SecurityException ok) {
}
joinPool(se);
}
/**
* schedule callable throws RejectedExecutionException if shutdown
*/
public void testSchedule3_RejectedExecutionException() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
se.shutdown();
se.schedule(new NoOpCallable(),
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(RejectedExecutionException success){
} catch (SecurityException ok) {
}
joinPool(se);
}
/**
* scheduleAtFixedRate throws RejectedExecutionException if shutdown
*/
public void testScheduleAtFixedRate1_RejectedExecutionException() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
se.shutdown();
se.scheduleAtFixedRate(new NoOpRunnable(),
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(RejectedExecutionException success){
} catch (SecurityException ok) {
}
joinPool(se);
}
/**
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown
*/
public void testScheduleWithFixedDelay1_RejectedExecutionException() {
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
try {
se.shutdown();
se.scheduleWithFixedDelay(new NoOpRunnable(),
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
shouldThrow();
} catch(RejectedExecutionException success){
} catch (SecurityException ok) {
}
joinPool(se);
}
/**
* getActiveCount increases but doesn't overestimate, when a
* thread becomes active
*/
public void testGetActiveCount() {
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
assertEquals(0, p2.getActiveCount());
p2.execute(new SmallRunnable());
try {
Thread.sleep(SHORT_DELAY_MS);
} catch(Exception e){
unexpectedException();
}
assertEquals(1, p2.getActiveCount());
joinPool(p2);
}
/**
* getCompletedTaskCount increases, but doesn't overestimate,
* when tasks complete
*/
public void testGetCompletedTaskCount() {
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
assertEquals(0, p2.getCompletedTaskCount());
p2.execute(new SmallRunnable());
try {
Thread.sleep(MEDIUM_DELAY_MS);
} catch(Exception e){
unexpectedException();
}
assertEquals(1, p2.getCompletedTaskCount());
joinPool(p2);
}
/**
* getCorePoolSize returns size given in constructor if not otherwise set
*/
public void testGetCorePoolSize() {
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
assertEquals(1, p1.getCorePoolSize());
joinPool(p1);
}
/**
* getLargestPoolSize increases, but doesn't overestimate, when
* multiple threads active
*/
public void testGetLargestPoolSize() {
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
assertEquals(0, p2.getLargestPoolSize());
p2.execute(new SmallRunnable());
p2.execute(new SmallRunnable());
try {
Thread.sleep(SHORT_DELAY_MS);
} catch(Exception e){
unexpectedException();
}
assertEquals(2, p2.getLargestPoolSize());
joinPool(p2);
}
/**
* getPoolSize increases, but doesn't overestimate, when threads
* become active
*/
public void testGetPoolSize() {
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
assertEquals(0, p1.getPoolSize());
p1.execute(new SmallRunnable());
assertEquals(1, p1.getPoolSize());
joinPool(p1);
}
/**
* getTaskCount increases, but doesn't overestimate, when tasks
* submitted
*/
public void testGetTaskCount() {
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
assertEquals(0, p1.getTaskCount());
for(int i = 0; i < 5; i++)
p1.execute(new SmallRunnable());
try {
Thread.sleep(SHORT_DELAY_MS);
} catch(Exception e){
unexpectedException();
}
assertEquals(5, p1.getTaskCount());
joinPool(p1);
}
/**
* getThreadFactory returns factory in constructor if not set
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?