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

Scala example source code file (RemoteGC.scala)

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

boolean, gc, hashset, nosuchobjectexception, referencequeue, referencequeue, remote, remote, remotegc, remotegc, rmi, string, string, t, weakreference

The Scala RemoteGC.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: RemoteGC.scala 17547 2009-04-21 13:56:28Z michelou $

package scala.runtime.remoting

import java.lang.ref.{Reference, WeakReference, ReferenceQueue}
import java.rmi.{NoSuchObjectException, Remote}
import java.rmi.server.UnicastRemoteObject

import scala.collection.mutable.HashSet

/**
 *
 *  @author Stephane Micheloud
 *  @version 1.0
 */
// Adapted from scala.actors.ActorGC
private [runtime] class RemoteGC {

  private val refQueue = new ReferenceQueue[Remote]
  private val refSet = new HashSet[Reference[T] forSome { type T <: Remote }]

  private var liveRefs = 0

  def newRef(a: Remote) = synchronized {
    refSet += new WeakReference(a, refQueue)
    liveRefs += 1
    info("added object reference \""+a+"\" ("+liveRefs+")")
  }

  def gc() = synchronized {
    info("GC called ("+liveRefs+")")
    // check for unreachable object references
    def drain() {
      val wr = refQueue.poll
      if (wr != null) {
        val msg = try {
          UnicastRemoteObject.unexportObject(wr.get, true/*force*/)
          "removed object reference"
        }
        catch {
          case e: NoSuchObjectException =>
            "object already unbound"
        }
        info(msg+" ("+liveRefs+")")
        liveRefs -= 1
        refSet -= wr
        // continue draining
        drain()
      }
    }
    drain()
  }

  def allClosed: Boolean = synchronized {
    liveRefs <= 0
  }

  private def info(msg: String) { Debug.info("[RemoteGC] "+msg) }
}

Other Scala examples (source code examples)

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