Class 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 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 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 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 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 Thread getThread​(Behaviour b)
        Returns:
        the Thread dedicated to the execution of the Behaviour b