How to use thread sleep in Scala

1 Answer

0 votes
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}

object UseThreadSleep_Scala {
  def main(args: Array[String]): Unit = {
    val start = System.currentTimeMillis()
    val sleepFuture = Future {
      Thread.sleep(1000)
    }

    sleepFuture.onComplete {
      case Success(_) =>
        println("Sleep time in ms = " + (System.currentTimeMillis() - start))
      case Failure(ie) =>
        println(ie)
    }

    Await.result(sleepFuture, Duration.Inf)
  }
}



/*
run:

Sleep time in ms = 1001
    
*/

 



answered Sep 1, 2024 by avibootz

Related questions

1 answer 114 views
114 views asked Sep 1, 2024 by avibootz
1 answer 109 views
109 views asked Sep 1, 2024 by avibootz
1 answer 106 views
106 views asked Sep 1, 2024 by avibootz
4 answers 2,722 views
2,722 views asked Mar 24, 2021 by avibootz
...