Package jade.core.behaviours
Class ThreadedBehaviourFactory
- java.lang.Object
-
- jade.core.behaviours.ThreadedBehaviourFactory
-
public class ThreadedBehaviourFactory extends 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 thewrap()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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classThreadedBehaviourFactory.ThreadedBehaviourWrapperInner class ThreadedBehaviourWrapper This class is declared public for debugging purpose only
-
Constructor Summary
Constructors Constructor Description ThreadedBehaviourFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ThreadgetThread(Behaviour b)ThreadedBehaviourFactory.ThreadedBehaviourWrapper[]getWrappers()This method is declared public for debugging purpose onlyvoidinterrupt()Interrupt all threaded behaviours managed by this ThreadedBehaviourFactoryThreadinterrupt(Behaviour b)Interrupt a threaded behaviour.voidresume(Behaviour b)Resume a threaded behaviour.intsize()voidsuspend(Behaviour b)Suspend a threaded behaviour.booleanwaitUntilEmpty(long timeout)Blocks until all threads dedicated to the execution of threaded behaviours complete.Behaviourwrap(Behaviour b)Wraps a normal JADE Behaviourbinto a "threaded behaviour".
-
-
-
Method Detail
-
wrap
public Behaviour wrap(Behaviour b)
Wraps a normal JADE Behaviourbinto a "threaded behaviour". Adding the wrapper behaviour to an agent results in executingbin 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:
trueif all threaded behaviour have actually completed,falseotherwise.
-
interrupt
public 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.
- Throws:
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 theresume()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 Thread getThread(Behaviour b)
- Returns:
- the Thread dedicated to the execution of the Behaviour
b
-
getWrappers
public ThreadedBehaviourFactory.ThreadedBehaviourWrapper[] getWrappers()
This method is declared public for debugging purpose only- Returns:
- All the wrapper behaviours currently used by this ThreadedBehaviourFactory
-
-