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

Scala example source code file (actor-receivewithin.scala)

This example Scala source code file (actor-receivewithin.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

a, a, actor, actor, b, b, fail1, fail2, test, timeout, timeout

The Scala actor-receivewithin.scala source code

import scala.actors.{Actor, TIMEOUT}

object A extends Actor {
  def act() {
    receive {
      case 'done =>
        var cnt = 0
        while (cnt < 500) {
          cnt += 1
          receiveWithin (0) {
            case 'msg =>
              if (cnt % 100 == 0)
                println("'msg")
            case TIMEOUT =>
              // should not happen
              println("FAIL1")
          }
        }
        cnt = 0
        while (cnt < 500) {
          cnt += 1
          receiveWithin (0) {
            case 'msg =>
              // should not happen
              println("FAIL2")
            case TIMEOUT =>
              if (cnt % 100 == 0)
                println("TIMEOUT")
          }
        }
        B ! 'next
        receive { case 'done => }
        cnt = 0
        while (cnt < 501) {
          cnt += 1
          receiveWithin (500) {
            case 'msg2 =>
              if (cnt % 100 == 0)
                println("'msg2")
            case TIMEOUT =>
              println("TIMEOUT")
          }
        }
    }
  }
}

object B extends Actor {
  def act() {
    A.start()
    for (_ <- 1 to 500) {
      A ! 'msg
    }
    A ! 'done
    receive {
      case 'next =>
        for (_ <- 1 to 500) {
          A ! 'msg2
        }
        A ! 'done
    }
  }
}

object Test {
  def main(args:Array[String]) {
    B.start()
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.