我在单例中创建以下执行程序:
final private ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
final ThreadFactory delegate = Executors.defaultThreadFactory();
public Thread newThread(Runnable paramAnonymousRunnable) {
Thread localThread = this.delegate.newThread(paramAnonymousRunnable);
localThread.setName("MyTask-" + localThread.getName());
localThread.setDaemon(XXX.this.daemonThread);
return localThread;
}
});
在程序执行期间,对单例的这种方法有很多调用。调用是在不同的线程中完成的,也许是在同一时间完成的。
private void send(final String paramString) {
try {
this.executor.execute(new Runnable() {
public void run() {
//DO some interesting stuff
}
});
} catch (Exception localException) {
this.handler.handle(localException);
}
}
在某些时候,以下堆栈开始出现:
java.util.concurrent.RejectedExecutionException
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:589)
at XXXXX.send(XXXX.java:269)
为什么jvm会抛出这样的异常?
singleThreadExecutor由LinkedBlockingQueue()支持 并且线程池没有关闭。
有关信息,jvm是oracle jdk 1.6。单身是用弹簧创造的。 从java.util.concurrent.Executors复制:
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
答案 0 :(得分:55)
execute
抛出RejectedExecutionException
由于您使用的是LinkedBlockingQueue
,我发现这种情况的唯一方法是关闭池。
答案 1 :(得分:4)
您可能在致电executor.shutdown()
后提交了任务。通常要停止执行者
executor.shutdown();
executor.awaitTermination(10, TimeUnit.MINUTES);
答案 2 :(得分:0)
也许您应该使用线程池而不是使用单个执行程序。
executor = new java.util.concurrent.ThreadPoolExecutor(30, 30, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
final AtomicInteger threadNumber = new AtomicInteger( 1 );
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "Thread No : " + threadNumber.getAndIncrement());
}
});
答案 3 :(得分:0)
线程不可用于执行给定任务。没有链接到任务的阻止队列。