jade.core.behaviours
Class ThreadedBehaviourFactory

java.lang.Object
  |
  +--jade.core.behaviours.ThreadedBehaviourFactory

public class ThreadedBehaviourFactory
extends java.lang.Object

This class provides support for executing JADE Behaviours in dedicated Java Threads. In order to do that it is sufficient to add to an agent a normal JADE Behaviour "wrapped" into a "threaded behaviour" as returned by the wrap() method of this class (see the example below).


 ThreadedBehaviourFactory tbf = new ThreadedBehaviourFactory();
 Behaviour b = // create a JADE behaviour
 addBehaviour(tbf.wrap(b));
 

This class also provides methods to control the termination of the threads dedicated to the execution of wrapped behaviours
NOT available in MIDP

Author:
Giovanni Caire - TILAB

Constructor Summary
ThreadedBehaviourFactory()
           
 
Method Summary
 java.lang.Thread getThread(Behaviour b)
           
 void interrupt()
          Interrupt all threaded behaviours managed by this ThreadedBehaviourFactory
 java.lang.Thread interrupt(Behaviour b)
          Interrupt a threaded behaviour.
 void resume(Behaviour b)
          Resume a threaded behaviour.
 int size()
           
 void suspend(Behaviour b)
          Suspend a threaded behaviour.
 boolean waitUntilEmpty(long timeout)
          Blocks until all threads dedicated to the execution of threaded behaviours complete.
 Behaviour wrap(Behaviour b)
          Wraps a normal JADE Behaviour b into a "threaded behaviour".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadedBehaviourFactory

public ThreadedBehaviourFactory()
Method Detail

wrap

public Behaviour wrap(Behaviour b)
Wraps a normal JADE Behaviour b into a "threaded behaviour". Adding the wrapper behaviour to an agent results in executing b in a dedicated Java Therad.


size

public int size()
Returns:
The number of active threads dedicated to the execution of wrapped behaviours.

interrupt

public void interrupt()
Interrupt all threaded behaviours managed by this ThreadedBehaviourFactory


waitUntilEmpty

public boolean waitUntilEmpty(long timeout)
Blocks until all threads dedicated to the execution of threaded behaviours complete.

Parameters:
timeout - The maximum timeout to wait for threaded behaviour termination.
Returns:
true if all threaded behaviour have actually completed, false otherwise.

interrupt

public java.lang.Thread interrupt(Behaviour b)
                           throws NotFoundException
Interrupt a threaded behaviour. This method should be used to abort a threaded behaviour instead of getThread().interrupt() because i) the latter may have no effect if called just after the threaded behaviour suspended itself and ii) the threaded behaviour may be suspended and in this case its Thread is null.

Returns:
the Thread that was interrupted if any.
NotFoundException

suspend

public void suspend(Behaviour b)
Suspend a threaded behaviour. This method has only effect if called by the threaded behaviour itself and has the effect of releasing its dedicated Java Thread. This can later be restored by means of the resume() method.


resume

public void resume(Behaviour b)
Resume a threaded behaviour. Assign a new Java Thread to a threaded behaviour that is currently suspended.


getThread

public java.lang.Thread getThread(Behaviour b)
Returns:
the Thread dedicated to the execution of the Behaviour b


JADE