alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t2515.scala)

This example Scala source code file (t2515.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

iteration, iteration, none, none, option, some, some, test, timeout, unit, unit

The Scala t2515.scala source code

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()
  }

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t2515.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.