Skip to end of metadata
Go to start of metadata

The Groovy implementation of the actors concept popularized mostly by Erlang or Scala has reached an important milestone. Version 0.6 of GParallelizer (http://code.google.com/p/gparallelizer/) has been made available for you to download, experiment and use.

Here's a short example of using actors to simulate a number guessing game:

import org.gparallelizer.actors.pooledActors.AbstractPooledActor

class GameMaster extends AbstractPooledActor {

      int secretNum

      void afterStart()
      {
              secretNum = new Random().nextInt(10)
      }

      void act()
      {
              loop
              {
                      react { int num ->
                              if      ( num > secretNum )
                                      reply 'too large'
                              else if ( num < secretNum )
                                      reply 'too small'
                              else
                              {
                                      reply 'you win'
                                      stop()
                              }
                      }
              }
      }
}


class Player extends AbstractPooledActor {
      String              name
      AbstractPooledActor server
      int                 myNum

      void act()
      {
              loop
              {
                      myNum = new Random().nextInt(10)

                      server.send myNum

                      react {
                              switch( it )
                              {
                                      case 'too large': println "$name: $myNum was too large"; break

                                      case 'too small': println "$name: $myNum was too small"; break

                                      case 'you win':   println "$name: I won $myNum"; stop(); break
                              }
                      }
              }
      }
}

def master = new GameMaster().start()
new Player( name: 'Player', server: master ).start()

The example given by Jordi Campos i Miralles, Departament de Matemàtica Aplicada i Anàlisi, MAiA Facultat de Matemàtiques, Universitat de Barcelona

The actors may internally share a thread pool, the size of which can be much smaller than the number of actors working on your algorithm and so saving your system valuable resources.
More examples are available at http://code.google.com/p/gparallelizer/.
The recent introductory blog post can be found at http://www.jroller.com/vaclav/entry/event_based_actors_in_groovy.

Now I'd like GParallelizer to face the reality and evolve further based on real needs. Don't hesitate to comment or discuss.

Václav Pech

Labels
  • None