【问题标题】:Scala Future callbacks not seem to be doing anythingScala Future 回调似乎没有做任何事情
【发布时间】:2018-04-03 04:54:54
【问题描述】:

我对 Scala 还是很陌生,正在玩 Futures。我有一小段代码,它与我在教程中找到的版本略有不同。

object Tmp extends App {

  val f = Future {
    val r = Random.nextInt(500)
    if (r < 499)
      throw new RuntimeException("bad value")
    r
  }
  f.onComplete(ff => {
    if (ff.isSuccess) {
      println(s"success ${ff.get}")
    }
  })
  f.failed.foreach(t => s"failure ${t.getMessage}")
  // do the rest of your work
  println("A ..."); Thread.sleep(100)
  println("B ..."); Thread.sleep(100)
  println("C ..."); Thread.sleep(100)
  println("D ..."); Thread.sleep(100)
  println("E ..."); Thread.sleep(100)
  println("F ..."); Thread.sleep(100)
  Thread.sleep(1000)
}

每次我运行这个,输出是:

A ...
B ...
C ...
D ...
E ...
F ...

我没有看到代码执行的成功/失败部分

【问题讨论】:

    标签: scala concurrency future


    【解决方案1】:

    失败被抛出,但你用if (ff.isSuccess) 忽略它。以下是您应该如何处理onComplete

    import scala.concurrent.Future
    import scala.util.{Failure, Success}
    import scala.util.Random
    import scala.concurrent.ExecutionContext.Implicits.global
    
    
    object Tmp extends App {
    
      val f = Future {
        val r = Random.nextInt(500)
        if (r < 499)
          throw new RuntimeException("bad value")
        r
      }
    
      f onComplete  {
        case Success(value) => value
        case Failure(e) => e.printStackTrace
      }
    
      // do the rest of your work
      println("A ..."); Thread.sleep(100)
      println("B ..."); Thread.sleep(100)
      println("C ..."); Thread.sleep(100)
      println("D ..."); Thread.sleep(100)
      println("E ..."); Thread.sleep(100)
      println("F ..."); Thread.sleep(100)
      Thread.sleep(1000)
    }
    

    【讨论】:

      【解决方案2】:

      您的代码引发异常,因为:

       if (r < 499)
        throw new RuntimeException("bad value")
      

      你看不到任何东西的原因

      f.failed.foreach(t => s"failure ${t.getMessage}") 
      

      是因为您只创建字符串,但不对其进行任何操作。试试这个:

      object Tmp extends App {
      
        val f = Future {
          val r = Random.nextInt(500)
          if (r < 499)
            throw new RuntimeException("bad value")
          r
       }
       f.onComplete(ff => {
         if (ff.isSuccess) {
           println(s"success ${ff.get}")
         }
         else{ //added this
           println(s"failes ${ff.get}")
         }
       })
       f.failed.foreach(t => println(s"failure ${t.getMessage}"))//and added this
       // do the rest of your work
       println("A ..."); Thread.sleep(100)
       println("B ..."); Thread.sleep(100)
       println("C ..."); Thread.sleep(100)
       println("D ..."); Thread.sleep(100)
       println("E ..."); Thread.sleep(100)
       println("F ..."); Thread.sleep(100)
       Thread.sleep(1000)
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-09
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 2019-11-26
        • 2019-12-08
        • 1970-01-01
        • 2018-10-04
        • 2020-12-21
        相关资源
        最近更新 更多