File: t2515.scala

package info (click to toggle)
scala 2.11.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 61,060 kB
  • ctags: 3,923
  • sloc: java: 13,251; xml: 3,214; sh: 1,553; python: 756; makefile: 40; awk: 36; ansic: 6
file content (43 lines) | stat: -rw-r--r-- 1,157 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import scala.actors.{Futures, TIMEOUT}
import scala.actors.Actor._

object Test {

  def compute(): Option[Boolean] = {
    val fts = for (j <- 0 until 5) yield Futures.future {
      receiveWithin (100) {
        case TIMEOUT => true
        case other   => false
      }
    }
    val done = Futures.awaitAll(2000, fts.toArray: _*) // list to array, as varargs
    if (done.contains(None))
      None
    else
      Some(true)
  }

  def main(args:Array[String]) : Unit = {
    val ft = Futures.future {
      val format = new java.text.DecimalFormat("000.00'ms'")
      var iter = 1
      val done = 11
      while (iter < done) {
        val start = System.nanoTime()
        val result = compute()
        val time = System.nanoTime() - start
        result match {
          case Some(result) =>
            //printf("Iteration %2d succeeded after %s %n", iter, format.format(time / 1e6))
            printf("Iteration %2d succeeded%n", iter)
            iter += 1
          case None =>
            printf(">>>> Iteration %2d failed after %s <<<<< %n", iter, format.format(time / 1e6))
            iter = done
        }
      }
    }
    ft()
  }

}