はScalaの2.12.xからscala.concurrent.Future.neverの実装です:scala Future.neverがCountDownLatchを使用する理由は何ですか?ここ
final object never extends Future[Nothing] {
@throws(classOf[TimeoutException])
@throws(classOf[InterruptedException])
override def ready(atMost: Duration)(implicit permit: CanAwait): this.type = {
atMost match {
case e if e eq Duration.Undefined => throw new IllegalArgumentException("cannot wait for Undefined period")
case Duration.Inf => new CountDownLatch(1).await()
case Duration.MinusInf => // Drop out
case f: FiniteDuration =>
if (f > Duration.Zero) new CountDownLatch(1).await(f.toNanos, TimeUnit.NANOSECONDS)
}
throw new TimeoutException(s"Future timed out after [$atMost]")
}
...
あなたはそれが現在のスレッドをブロックするnew CountDownLatch(1).await()
を使用して見ることができるように。なぜそれはThread.sleep()
よりも優れていますか?