加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Scala期货时如何捕获RejectedExecutionException?

发布时间:2020-12-16 18:12:05 所属栏目:安全 来源:网络整理
导读:关闭执行程序时,我应该在哪里捕获RejectedExecutionExceptions?我试过了: future { Option(reader.readLine) } onComplete { case Success(v) = case Failure(e) = e match { case ree: RejectedExecutionException = // doesn't work } 并且: try { futu
关闭执行程序时,我应该在哪里捕获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或您的自定义实现.

此执行程序以静默方式传递RejectedExecutionException:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读