当前位置:网站首页>Simple use of future in Scala

Simple use of future in Scala

2020-11-07 23:58:00 read

scala Future

object MyFuture {

  def doWork(i: Int): Int = {
    Thread.sleep(3 * 1000)
    i
  }

  def main(args: Array[String]): Unit =
    1 to 5 foreach { i =>
      val future = Future {
        blocking {
          doWork(i)
        }
      }
      future onComplete {
        case Success(value) => println(value)
        case Failure(exception) => println(exception)
      }
      //  Through horizon transformation , Give Way  Int  Have a richer approach 
      //  By inheritance  AnyVal, That means this is one  value class
      /*
      implicit final class DurationInt(private val n: Int) extends AnyVal with DurationConversions {
        override protected def durationIn(unit: TimeUnit): FiniteDuration = Duration(n.toLong, unit)
      }
       */
      Await.result(future, 7 seconds)
    }
}

版权声明
本文为[read]所创,转载请带上原文链接,感谢