ThreadPoolExcuter的queue测试
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadTest {
public static void main(String[] args) {
//extracted(new LinkedBlockingDeque<Runnable>());
//extracted(new ArrayBlockingQueue<Runnable>(5));
extracted(new SynchronousQueue<Runnable>());
}
private static void extracted(BlockingQueue<Runnable> workQueue) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 2, TimeUnit.MINUTES, workQueue);
for (int i = 0; i < 10; i++) {
try {
executor.execute(new Runnable() {
@Override
public void run() {
System.out.println(System.currentTimeMillis()+" "+Thread.currentThread()+"...开始处理,此时workQueue.size:"+workQueue.size());
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(System.currentTimeMillis()+" "+Thread.currentThread()+"...处理完成");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
LinkedBlockingDeque*,不使用非核心现场
1632557625328 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:9
1632557628341 Thread[pool-1-thread-1,5,main]...处理完成
1632557628342 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:8
1632557631342 Thread[pool-1-thread-1,5,main]...处理完成
1632557631342 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:7
1632557634344 Thread[pool-1-thread-1,5,main]...处理完成
1632557634344 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:6
1632557637346 Thread[pool-1-thread-1,5,main]...处理完成
1632557637346 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:5
1632557640349 Thread[pool-1-thread-1,5,main]...处理完成
1632557640349 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:4
1632557643352 Thread[pool-1-thread-1,5,main]...处理完成
1632557643352 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:3
1632557646353 Thread[pool-1-thread-1,5,main]...处理完成
1632557646353 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:2
1632557649360 Thread[pool-1-thread-1,5,main]...处理完成
1632557649360 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:1
1632557652364 Thread[pool-1-thread-1,5,main]...处理完成
1632557652364 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:0
1632557655366 Thread[pool-1-thread-1,5,main]...处理完成
ArrayBlockingQueue有界,使用了非核心线程,超出报错.
1632557860510 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:5
1632557860510 Thread[pool-1-thread-2,5,main]...开始处理,此时workQueue.size:5
java.util.concurrent.RejectedExecutionException
at org.apache.tomcat.util.threads.ThreadPoolExecutor$RejectHandler.rejectedExecution(ThreadPoolExecutor.java:221)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:167)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:145)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException
at org.apache.tomcat.util.threads.ThreadPoolExecutor$RejectHandler.rejectedExecution(ThreadPoolExecutor.java:221)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:167)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:145)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException
at org.apache.tomcat.util.threads.ThreadPoolExecutor$RejectHandler.rejectedExecution(ThreadPoolExecutor.java:221)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:167)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:145)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
1632557863511 Thread[pool-1-thread-1,5,main]...处理完成
1632557863511 Thread[pool-1-thread-2,5,main]...处理完成
1632557863513 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:3
1632557863513 Thread[pool-1-thread-2,5,main]...开始处理,此时workQueue.size:3
1632557866514 Thread[pool-1-thread-2,5,main]...处理完成
1632557866514 Thread[pool-1-thread-1,5,main]...处理完成
1632557866514 Thread[pool-1-thread-2,5,main]...开始处理,此时workQueue.size:2
1632557866514 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:1
1632557869527 Thread[pool-1-thread-2,5,main]...处理完成
1632557869527 Thread[pool-1-thread-1,5,main]...处理完成
SynchronousQueue 会使用非核心线程,但没有队列,超出报错
1632558148047 Thread[pool-1-thread-1,5,main]...开始处理,此时workQueue.size:0
1632558148047 Thread[pool-1-thread-2,5,main]...开始处理,此时workQueue.size:0
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@5c647e05 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@3d4eac69 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@75b84c92 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@232204a1 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@7d4991ad rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@1b6d3586 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@74a14482 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
java.util.concurrent.RejectedExecutionException: Task ThreadTest$1@677327b6 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at ThreadTest.extracted(ThreadTest.java:23)
at ThreadTest.main(ThreadTest.java:14)
1632558151049 Thread[pool-1-thread-1,5,main]...处理完成
1632558151049 Thread[pool-1-thread-2,5,main]...处理完成