开发者

RejectedExecutionException even when ExecutorService is guarded by isShutdown()

I've got a problem that's been driving me nuts for a while and I need to consult your collective wisdom.

I have a ThreadPoolExecutor with a pool of 1, called from Executors.newFixedThreadPool. I guard all executor.execute() wit开发者_如何学Pythonh !isShutdown() since executor.shutdown() is called from another thread, and so should be able to say that the executor will not throw a RejectedExecutionException. Nevertheless, this is what I'm seeing randomly.

Does anyone have a good idea why I'm seeing this behaviour?


You might go through an article on double-checked locking in order to understand how/why an unsynchronized "if then act" approach doesn't work in java.

Out of curiosity, why do you bother to check if the work will be rejected? I can't remember seeing this done before. Instead, why not catch and handle any RejectedExecutionExceptions that are thrown?

Also be careful of how you're handling other error conditions, especially tasks that are running or waiting in the queue when shutdown is called. Chapter 7 of Java Concurrency in Practice has lots of good information about this.


There are other reasons for rejected execution that are spelled out here http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html

Most likely your queue reached it's bounds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜