Java:同步(对象)和RejectedExecutionException
我有这个问题:
我有几个线程使用 synchronized(Object) { ... }
访问一个对象,
但有时会引发此异常:
execute: java.util.concurrent.RejectedExecutionException为什么
?我应该用它做什么?
谢谢
I have this problem:
I have a few threads which access one object with synchronized(Object) { ... }
But sometimes this exception is raised:
execute: java.util.concurrent.RejectedExecutionException
Why? And what should I do with it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常是由 Executor.execute 引发的 - 您当然不应该仅仅通过访问同步块中的对象来看到它。
查看堆栈跟踪的其余部分,以准确找出它发生的位置,并查看消息,看看这是否为您提供了有关获取该消息的原因的更多信息。正如托德评论的那样,这通常是由于工作队列已满之类的原因。
例如,如果队列用于缓冲要运行的任务拒绝接受更多项目,则 ThreadPoolExecutor 将抛出此异常。这通常表明您的系统过载,或者您错误配置了执行器。
That exception is meant to be raised by
Executor.execute
- you certainly shouldn't be seeing it just by accessing objects in a synchronized block.Have a look at the rest of the stack trace to work out exactly where it's occurring, and look at the message to see if that gives you any more information about why you're getting it. As Todd commented, it would usually be due to something like a full work queue.
For example,
ThreadPoolExecutor
will throw this exception if the queue is uses to buffer tasks to be run refuses to accept more items. This usually indicates that your system is overloaded, or you've misconfigured the executor.