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

Scala example source code file (actorgc_leak.scala)

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

actor, actor, actorgc, array, array, done, fatactor, fatactor, fatactorfactory, fatactors, gc, monitor, monitor, test

The Scala actorgc_leak.scala source code


import scala.actors.Actor

object Test {
  class FatActorFactory extends Actor {
    def act() {
      var cnt = 0
      Actor.loopWhile(cnt < fatActors) {
	//if ((cnt % 5) == 0) println(cnt)
	val fa = new FatActor()
	fa.start()
	cnt += 1
	if (cnt == fatActors) Monitor ! 'done
      }
    }
  }
  
  class FatActor extends Actor {
    def act() {
      fat = new Array[Int](fatness)
      react {
	case 'hi => exit()
      }
    }
    private var fat: Array[Int] = _
  }

  object Monitor extends Actor {
    private var cnt = 0
    def act() {
      Actor.loop {
	react {
	  case 'done => {
	    cnt += 1
	    if (cnt == factories) System.exit(0) // once GC pressure stops FatActors stop being collected, and as
	  }                                     // a result ActorGC never finds out that they are defunct
	}
      }
    }
  }

  val factories = 4   // the number of factories to start
  val fatActors = 50  // the number of FatActors for each factory to produce
  val fatness = 1024*1024*10

  def main(args: Array[String]) {
    scala.actors.Scheduler.impl.shutdown()
    val sched = {
      val s = new scala.actors.FJTaskScheduler2
      s.start()
      s
    }
    scala.actors.Scheduler.impl = sched

    Monitor.start()
    for(i <- 1 to factories) {
      //if ((i % 50) == 0) println(i)
      val fa = new FatActorFactory()
      fa.start()
    }
    println("Done")
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala actorgc_leak.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.