gpt4 book ai didi

scala - 使用 Scala future 时如何捕获 RejectedExecutionException?

转载 作者:行者123 更新时间:2023-12-04 23:55:05 24 4
gpt4 key购买 nike

关闭执行程序时,我应该在哪里捕获 RejectedExecutionExceptions?我试过:

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

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

也不起作用。仍然得到:
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)

最佳答案

它必须由 RejectedExecutionHandler 处理。要么java.util.concurrent.ThreadPoolExecutor.DiscardPolicy或通过您的自定义实现。

这个 executor 悄悄地传递了 RejectedExecutionException:

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

关于scala - 使用 Scala future 时如何捕获 RejectedExecutionException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17782375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com