Andrey
Andrey

Reputation: 201

Java: synchronized(Object) and 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

Upvotes: 1

Views: 468

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500575

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.

Upvotes: 2

Related Questions