groovyx.gpars.actor
Class Actors

java.lang.Object
  extended by groovyx.gpars.actor.Actors

public abstract class Actors
extends java.lang.Object

Provides handy helper methods to create pooled actors and customize the underlying thread pool. Use static import to be able to call Actors methods without the need to prepend them with the Actors identifier.

 import static org.gpars.actor.Actors.*

 Actors.defaultActorPGroup.resize 1

 def actor = actor {*     react {message ->
         println message
 }*     //this line will never be reached
 }.start()

 actor.send 'Hi!'
 

All actors created through the Actors class will belong to the same default actor group and run on daemon threads. The DefaultPGroup class should be used when actors need to be grouped into multiple groups or when non-daemon threads are to be used.

Author:
Vaclav Pech, Alex Tkachman Date: Feb 18, 2009

Field Summary
static DefaultPGroup defaultActorPGroup
          The default actor group to share by all actors created through the Actors class.
 
Constructor Summary
Actors()
           
 
Method Summary
static DefaultActor actor(java.lang.Runnable handler)
          Creates a new instance of DefaultActor, using the passed-in closure as the body of the actor's act() method.
static BlockingActor blockingActor(java.lang.Runnable handler)
          Creates a new instance of BlockingActor, using the passed-in closure as the body of the actor's act() method.
static DefaultActor fairActor(java.lang.Runnable handler)
          Creates a new instance of PooledActor, using the passed-in closure as the body of the actor's act() method.
static Actor fairMessageHandler(groovy.lang.Closure code)
          Creates an instance of DynamicDispatchActor, which will cooperate in thread sharing with other actors in a fair manner.
static Actor fairReactor(groovy.lang.Closure code)
          Creates a reactor around the supplied code, which will cooperate in thread sharing with other actors in a fair manner.
static Actor fairStaticMessageHandler(groovy.lang.Closure code)
          Creates an instance of StaticDispatchActor, which will cooperate in thread sharing with other actors sharing the same thread pool.
static Actor messageHandler(groovy.lang.Closure code)
          Creates an instance of DynamicDispatchActor.
static Actor reactor(groovy.lang.Closure code)
          Creates a reactor around the supplied code.
static Actor staticMessageHandler(groovy.lang.Closure code)
          Creates an instance of StaticDispatchActor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultActorPGroup

public static final DefaultPGroup defaultActorPGroup
The default actor group to share by all actors created through the Actors class.

Constructor Detail

Actors

public Actors()
Method Detail

actor

public static DefaultActor actor(java.lang.Runnable handler)
Creates a new instance of DefaultActor, using the passed-in closure as the body of the actor's act() method. The created actor will be part of the default actor group.

Parameters:
handler - The body of the newly created actor's act method.
Returns:
A newly created instance of the DefaultActor class

blockingActor

public static BlockingActor blockingActor(java.lang.Runnable handler)
Creates a new instance of BlockingActor, using the passed-in closure as the body of the actor's act() method. The created actor will be part of the default actor group.

Parameters:
handler - The body of the newly created actor's act method.
Returns:
A newly created instance of the BlockingActor class

fairActor

public static DefaultActor fairActor(java.lang.Runnable handler)
Creates a new instance of PooledActor, using the passed-in closure as the body of the actor's act() method. The created actor will be part of the default actor group. The actor will cooperate in thread sharing with other actors sharing the same thread pool in a fair manner.

Parameters:
handler - The body of the newly created actor's act method.
Returns:
A newly created instance of the DefaultActor class

reactor

public static Actor reactor(groovy.lang.Closure code)
Creates a reactor around the supplied code. When a reactor receives a message, the supplied block of code is run with the message as a parameter and the result of the code is send in reply. The created actor will be part of the default actor group.

Parameters:
code - The code to invoke for each received message
Returns:
A new instance of ReactiveEventBasedThread

fairReactor

public static Actor fairReactor(groovy.lang.Closure code)
Creates a reactor around the supplied code, which will cooperate in thread sharing with other actors in a fair manner. When a reactor receives a message, the supplied block of code is run with the message as a parameter and the result of the code is send in reply. The created actor will be part of the default actor group.

Parameters:
code - The code to invoke for each received message
Returns:
A new instance of ReactiveEventBasedThread

messageHandler

public static Actor messageHandler(groovy.lang.Closure code)
Creates an instance of DynamicDispatchActor.

Parameters:
code - The closure specifying individual message handlers.
Returns:
A new started instance of a DynamicDispatchActor

fairMessageHandler

public static Actor fairMessageHandler(groovy.lang.Closure code)
Creates an instance of DynamicDispatchActor, which will cooperate in thread sharing with other actors in a fair manner.

Parameters:
code - The closure specifying individual message handlers.
Returns:
A new started instance of a fair DynamicDispatchActor

staticMessageHandler

public static Actor staticMessageHandler(groovy.lang.Closure code)
Creates an instance of StaticDispatchActor.

Parameters:
code - The closure specifying the only statically dispatched message handler.
Returns:
A new started instance of a StaticDispatchActor

fairStaticMessageHandler

public static Actor fairStaticMessageHandler(groovy.lang.Closure code)
Creates an instance of StaticDispatchActor, which will cooperate in thread sharing with other actors sharing the same thread pool.

Parameters:
code - The closure specifying the only statically dispatched message handler.
Returns:
A new started instance of a fair StaticDispatchActor

Copyright © 2008–2012 Václav Pech. All Rights Reserved.