text Scala Future.sequence示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Scala Future.sequence示例相关的知识,希望对你有一定的参考价值。

/**
 * @author sjalipar
 */
object myMain {
  def main(args: Array[String]): Unit = {

    println("test - start")
    import scala.concurrent.Future
    import scala.util.Success
    import scala.util.Failure
    import scala.concurrent.Await
    import scala.concurrent.duration.DurationInt
    import scala.concurrent.ExecutionContext.Implicits.global

    val x1 = Future {
      Thread.sleep(2000)
      println("x1 is completed")
      "Hello x1"
    }

    val x2 = Future {
      Thread.sleep(4000)
      println("x2 is completed")
      throw new Exception("Exception occured with x2")
    }

    val x3 = Future {
      Thread.sleep(10000)
      println("x3 is completed")
      "Hello x3"
    }

    val x = Future.sequence(List(x1, x2, x3))

    x.onComplete {
      case Success(res) => println("Success: " + res)
      case Failure(ex)  => println("Ohhh Exception: " + ex.getMessage)
    }

   Await.ready(x, 20.seconds)

    println(s"Future val is: ${x.value}")
    Thread.sleep(2000)
    println("test - end")
  }
}

以上是关于text Scala Future.sequence示例的主要内容,如果未能解决你的问题,请参考以下文章

text Scala笔记

第一天学习scala:scala字符串操作

2.scala控制结构函数异常处理

如何编写hadoop map减少scala中的程序

learning scala read from file

Proguard 警告:找不到引用的类 scala。*