4

Where should I catch RejectedExecutionExceptions when shutting down the executor ? I tried:

 future {
        Option(reader.readLine)
      } onComplete {
        case Success(v) =>
        case Failure(e) => e match {
          case ree: RejectedExecutionException =>
          // doesn't work
      }

and :

 try {
        future {
          Option(reader.readLine)
        } onComplete {
          ...
        }
      } catch {
        case ree: RejectedExecutionException =>
          // doesn't work
      }

also doesn't work. Still getting :

Exception in thread "pool-99-thread-1" java.util.concurrent.RejectedExecutionException
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1768)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:658)
at scala.concurrent.impl.ExecutionContextImpl.execute(ExecutionContextImpl.scala:105)
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:37)
at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:133)
at scala.concurrent.Promise$class.complete(Promise.scala:55)
at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:58)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

2 Answers 2

5

It must be handled by RejectedExecutionHandler. Either java.util.concurrent.ThreadPoolExecutor.DiscardPolicy or by your custom implementation.

This executor silently passes over RejectedExecutionException:

val executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue[Runnable], Executors.defaultThreadFactory, new DiscardPolicy)
2

I asked about that in this thread.

I felt that currently registered callbacks should be finished at shutdown, if they are running on the same executor. (Or the behavior should be managed by policy.) (Someday I'll submit a PR for what I described, since it seems natural enough.)

I previously had a setup where a single-threaded executor read some files and fed jobs to another pool. The initial design was for the feeder to throttle itself by blocking on submit. That was OK, but inflexible. I wanted to fork some tasks on the second pool, but of course submits would block there.

So, given that blocking is evil, you have to decide what to do with your tasks once you know you can't run them.

One answer is that you wait for quiescence before initiating shutdown. In my case, I had a count of jobs to complete, so I knew when work was done.

Viktor Klang's point in that thread was that tasks are forked and submitted all the time, so if anyone knows what quiescence means, it's the app, not the infrastructure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.