org.codehaus.gpars

groovyx.gpars.agent
[Java] Class Agent

java.lang.Object
  groovyx.gpars.agent.AgentCore
      groovyx.gpars.agent.AgentBase
          groovyx.gpars.agent.Agent

@java.lang.SuppressWarningspublic Agent(final T data) {
public class Agent
extends AgentBase

A special-purpose thread-safe non-blocking reference implementation inspired by Agents in Clojure. Agents safe-guard mutable values by allowing only a single agent-managed thread to make modifications to them. The mutable values are not directly accessible from outside, but instead requests have to be sent to the agent and the agent guarantees to process the requests sequentially on behalf of the callers. Agents guarantee sequential execution of all requests and so consistency of the values. An agent wraps a reference to mutable state, held inside a single field, and accepts code (closures / commands) as messages, which can be sent to the Agent just like to any other actor using the '<<' operator or any of the send() methods. After reception of a closure / command, the closure is invoked against the internal mutable field. The closure is guaranteed to be run without intervention from other threads and so may freely alter the internal state of the Agent held in the internal data field. The return value of the submitted closure is sent in reply to the sender of the closure. If the message sent to an agent is not a closure, it is considered to be a new value for the internal reference field. The internal reference can also be changed using the updateValue() method from within the received closures. The 'val' property of an agent will safely return the current value of the Agent, while the valAsync() method will do the same without blocking the caller. The 'instantVal' property will retrieve the current value of the Agent without having to wait in the queue of tasks. The initial internal value can be passed to the constructor. The two-parameter constructor allows to alter the way the internal value is returned from val/valAsync. By default the original reference is returned, but in many scenarios a copy or a clone might be more appropriate.

Authors:
Vaclav Pech Date: Jul 2, 2009


Field Summary
 
Fields inherited from class AgentBase
awaitClosure, copy, data, listeners, lock, validators
 
Fields inherited from class AgentCore
core, currentThread, errors
 
Constructor Summary
Agent()

Agent(java.lang.Object data)

Agent(java.lang.Object data, groovy.lang.Closure copy)

 
Method Summary
static Agent agent(java.lang.Object state)

Creates an agent instance initialized with the given state.

static Agent agent(java.lang.Object state, groovy.lang.Closure copy)

Creates an agent instance initialized with the given state, which will cooperate in thread sharing with other Agent instances in a fair manner.

static Agent fairAgent(java.lang.Object state)

Creates an agent instance initialized with the given state, which will cooperate in thread sharing with other agents and actors in a fair manner.

static Agent fairAgent(java.lang.Object state, groovy.lang.Closure copy)

void handleMessage(java.lang.Object message)

Creates an agent instance initialized with the given state.

 
Methods inherited from class AgentBase
addListener, addValidator, await, checkClosure, getInstantVal, getVal, onMessage, onMessage, onMessage, sendAndWait, updateValue, valAsync
 
Methods inherited from class AgentCore
attachToThreadPool, call, getErrors, handleMessage, hasErrors, isFair, leftShift, makeFair, registerError, send, setPGroup
 

Constructor Detail

Agent

public Agent()


Agent

public Agent(java.lang.Object data)


Agent

public Agent(java.lang.Object data, groovy.lang.Closure copy)


 
Method Detail

agent

@java.lang.SuppressWarningsreturn Actors.defaultActorPGroup.agent(state, copy);
public static Agent agent(java.lang.Object state)
Creates an agent instance initialized with the given state. The instance will use the default thread pool.
Parameters:
state - The initial internal state of the new Agent instance
copy - A closure to use to create a copy of the internal state when sending the internal state out
Returns:
The created instance


agent

@java.lang.SuppressWarningsreturn Actors.defaultActorPGroup.fairAgent(state);
public static Agent agent(java.lang.Object state, groovy.lang.Closure copy)
Creates an agent instance initialized with the given state, which will cooperate in thread sharing with other Agent instances in a fair manner. The instance will use the default thread pool.
Parameters:
state - The initial internal state of the new Agent instance
Returns:
The created instance


fairAgent

@java.lang.SuppressWarningsreturn Actors.defaultActorPGroup.fairAgent(state, copy);
public static Agent fairAgent(java.lang.Object state)
Creates an agent instance initialized with the given state, which will cooperate in thread sharing with other agents and actors in a fair manner. The instance will use the default thread pool.
Parameters:
state - The initial internal state of the new Agent instance
copy - A closure to use to create a copy of the internal state when sending the internal state out
Returns:
The created instance


fairAgent

@java.lang.SuppressWarnings
public static Agent fairAgent(java.lang.Object state, groovy.lang.Closure copy)


handleMessage

@java.lang.SuppressWarnings({"unchecked"})
@java.lang.Overridepublic static  Agent agent(final T state) {
public void handleMessage(java.lang.Object message)
Creates an agent instance initialized with the given state. The instance will use the default thread pool.
Parameters:
state - The initial internal state of the new Agent instance
Returns:
The created instance


 

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