threadpoolexecutortest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,573 行 · 第 1/4 页
JAVA
1,573 行
for(int i = 0; i < 5; i++){
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
p1.execute(tasks[i]);
}
tasks[4].cancel(true);
tasks[3].cancel(true);
p1.purge();
long count = p1.getTaskCount();
assertTrue(count >= 2 && count < 5);
joinPool(p1);
}
/**
* shutDownNow returns a list containing tasks that were not run
*/
public void testShutDownNow() {
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
List l;
try {
for(int i = 0; i < 5; i++)
p1.execute(new MediumPossiblyInterruptedRunnable());
}
finally {
try {
l = p1.shutdownNow();
} catch (SecurityException ok) { return; }
}
assertTrue(p1.isShutdown());
assertTrue(l.size() <= 4);
}
// Exception Tests
/**
* Constructor throws if corePoolSize argument is less than zero
*/
public void testConstructor1() {
try {
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is less than zero
*/
public void testConstructor2() {
try {
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is equal to zero
*/
public void testConstructor3() {
try {
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if keepAliveTime is less than zero
*/
public void testConstructor4() {
try {
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if corePoolSize is greater than the maximumPoolSize
*/
public void testConstructor5() {
try {
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if workQueue is set to null
*/
public void testConstructorNullPointerException() {
try {
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if corePoolSize argument is less than zero
*/
public void testConstructor6() {
try {
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory());
shouldThrow();
} catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is less than zero
*/
public void testConstructor7() {
try {
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is equal to zero
*/
public void testConstructor8() {
try {
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if keepAliveTime is less than zero
*/
public void testConstructor9() {
try {
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if corePoolSize is greater than the maximumPoolSize
*/
public void testConstructor10() {
try {
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if workQueue is set to null
*/
public void testConstructorNullPointerException2() {
try {
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if threadFactory is set to null
*/
public void testConstructorNullPointerException3() {
try {
ThreadFactory f = null;
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f);
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if corePoolSize argument is less than zero
*/
public void testConstructor11() {
try {
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is less than zero
*/
public void testConstructor12() {
try {
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is equal to zero
*/
public void testConstructor13() {
try {
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if keepAliveTime is less than zero
*/
public void testConstructor14() {
try {
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if corePoolSize is greater than the maximumPoolSize
*/
public void testConstructor15() {
try {
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if workQueue is set to null
*/
public void testConstructorNullPointerException4() {
try {
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if handler is set to null
*/
public void testConstructorNullPointerException5() {
try {
RejectedExecutionHandler r = null;
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),r);
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if corePoolSize argument is less than zero
*/
public void testConstructor16() {
try {
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is less than zero
*/
public void testConstructor17() {
try {
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if maximumPoolSize is equal to zero
*/
public void testConstructor18() {
try {
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if keepAliveTime is less than zero
*/
public void testConstructor19() {
try {
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if corePoolSize is greater than the maximumPoolSize
*/
public void testConstructor20() {
try {
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (IllegalArgumentException success){}
}
/**
* Constructor throws if workQueue is set to null
*/
public void testConstructorNullPointerException6() {
try {
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if handler is set to null
*/
public void testConstructorNullPointerException7() {
try {
RejectedExecutionHandler r = null;
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),new SimpleThreadFactory(),r);
shouldThrow();
}
catch (NullPointerException success){}
}
/**
* Constructor throws if ThreadFactory is set top null
*/
public void testConstructorNullPointerException8() {
try {
ThreadFactory f = null;
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f,new NoOpREHandler());
shouldThrow();
}
catch (NullPointerException successdn8){}
}
/**
* execute throws RejectedExecutionException
* if saturated.
*/
public void testSaturatedExecute() {
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1));
try {
for(int i = 0; i < 5; ++i){
p.execute(new MediumRunnable());
}
shouldThrow();
} catch(RejectedExecutionException success){}
joinPool(p);
}
/**
* executor using CallerRunsPolicy runs task if saturated.
*/
public void testSaturatedExecute2() {
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h);
try {
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
for(int i = 0; i < 5; ++i){
tasks[i] = new TrackedNoOpRunnable();
}
TrackedLongRunnable mr = new TrackedLongRunnable();
p.execute(mr);
for(int i = 0; i < 5; ++i){
p.execute(tasks[i]);
}
for(int i = 1; i < 5; ++i) {
assertTrue(tasks[i].done);
}
try { p.shutdownNow(); } catch(SecurityException ok) { return; }
} catch(RejectedExecutionException ex){
unexpectedException();
} finally {
joinPool(p);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?