groovyx.gpars.util
Class AsyncMessagingCore

java.lang.Object
  extended by groovyx.gpars.util.AsyncMessagingCore
All Implemented Interfaces:
java.lang.Runnable

public abstract class AsyncMessagingCore
extends java.lang.Object
implements java.lang.Runnable

Author:
Vaclav Pech Date: Aug 23, 2010

Field Summary
private  int active
          Indicates, whether there's an active thread handling a message inside the agent's body
private static int ACTIVE
           
private static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AsyncMessagingCore> activeUpdater
           
private  boolean fair
          Fair agents give up the thread after processing each message, non-fair agents keep a thread until their message queue is empty.
private static int PASSIVE
           
private  MessageQueue queue
          Incoming messages
private  Pool threadPool
           
 
Constructor Summary
protected AsyncMessagingCore(Pool threadPool)
           
 
Method Summary
 void attachToThreadPool(Pool threadPool)
          Sets a new thread pool to be used by the agent
protected  boolean continueProcessingMessages()
          Informs about a new thread being assigned to process the next message
(package private)  Pool getThreadPool()
           
protected abstract  void handleMessage(java.lang.Object message)
           
 boolean isFair()
          Retrieves the agent's fairness flag Fair agents give up the thread after processing each message, non-fair agents keep a thread until their message queue is empty.
 void makeFair()
          Makes the agent fair.
protected abstract  void registerError(java.lang.Throwable e)
           
 void run()
          Handles a single message from the message queue
(package private)  void schedule()
          Schedules processing of a next message, if there are some and if there isn't an active thread handling a message at the moment
 void store(java.lang.Object message)
          Adds the message to the agent\s message queue
 java.lang.Object sweepNextMessage()
          Removes the head of the message queue
protected  void threadAssigned()
          Informs about the thread being removed from the task
protected  void threadUnassigned()
          Informs about a new thread being assigned to process the next message
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadPool

private Pool threadPool

fair

private volatile boolean fair
Fair agents give up the thread after processing each message, non-fair agents keep a thread until their message queue is empty.


queue

private final MessageQueue queue
Incoming messages


active

private volatile int active
Indicates, whether there's an active thread handling a message inside the agent's body


activeUpdater

private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AsyncMessagingCore> activeUpdater

PASSIVE

private static final int PASSIVE
See Also:
Constant Field Values

ACTIVE

private static final int ACTIVE
See Also:
Constant Field Values
Constructor Detail

AsyncMessagingCore

protected AsyncMessagingCore(Pool threadPool)
Method Detail

attachToThreadPool

public final void attachToThreadPool(Pool threadPool)
Sets a new thread pool to be used by the agent

Parameters:
threadPool - The thread pool to use

getThreadPool

Pool getThreadPool()

isFair

public boolean isFair()
Retrieves the agent's fairness flag Fair agents give up the thread after processing each message, non-fair agents keep a thread until their message queue is empty. Non-fair agents tend to perform better than fair ones.

Returns:
True for fair agents, false for non-fair ones. Agents are non-fair by default.

makeFair

public void makeFair()
Makes the agent fair. Agents are non-fair by default. Fair agents give up the thread after processing each message, non-fair agents keep a thread until their message queue is empty. Non-fair agents tend to perform better than fair ones.


store

public void store(java.lang.Object message)
Adds the message to the agent\s message queue

Parameters:
message - A value or a closure

schedule

void schedule()
Schedules processing of a next message, if there are some and if there isn't an active thread handling a message at the moment


sweepNextMessage

public java.lang.Object sweepNextMessage()
Removes the head of the message queue

Returns:
The head message, or null, if the message queue is empty

run

public final void run()
Handles a single message from the message queue

Specified by:
run in interface java.lang.Runnable

continueProcessingMessages

protected boolean continueProcessingMessages()
Informs about a new thread being assigned to process the next message

Returns:
True, if the calculation should proceed

threadUnassigned

protected void threadUnassigned()
Informs about a new thread being assigned to process the next message


threadAssigned

protected void threadAssigned()
Informs about the thread being removed from the task


registerError

protected abstract void registerError(java.lang.Throwable e)

handleMessage

protected abstract void handleMessage(java.lang.Object message)

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