Class Messenger

  • All Implemented Interfaces:
    java.lang.Runnable

    public class Messenger
    extends java.lang.Object
    implements java.lang.Runnable

    This class implements a single thread that is able to process arbitrary tasks. Tasks are enqueued using the synchronized enqueue(Task) method, and are run in the order they were enqueued.

    Author:
    Simon Thoresen Hult
    • Constructor Detail

      • Messenger

        public Messenger()
    • Method Detail

      • start

        public void start()

        Starts the internal thread. This must be done AFTER all recurrent tasks have been added.

        See Also:
        addRecurrentTask(Task)
      • deliverMessage

        public void deliverMessage​(Message msg,
                                   MessageHandler handler)

        Convenience method to post a Messenger.Task that delivers a Message to a MessageHandler to the queue of tasks to be executed.

        Parameters:
        msg - The message to send.
        handler - The handler to send to.
      • deliverReply

        public void deliverReply​(Reply reply,
                                 ReplyHandler handler)

        Convenience method to post a Messenger.Task that delivers a Reply to a ReplyHandler to the queue of tasks to be executed.

        Parameters:
        reply - The reply to return.
        handler - The handler to return to.
      • enqueue

        public void enqueue​(Messenger.Task task)

        Enqueues the given task in the list of tasks that this worker is to process. If this thread has been destroyed previously, this method invokes Messenger.Task.destroy().

        Parameters:
        task - The task to enqueue.
      • sync

        public void sync()

        Handshakes with the internal thread. If this method is called using the messenger thread, this will deadlock.

      • destroy

        public boolean destroy()

        Sets the destroyed flag to true. The very first time this method is called, it cleans up all its dependencies. Even if you retain a reference to this object, all of its content is allowed to be garbage collected.

        Returns:
        True if content existed and was destroyed.
      • run

        public void run()
        Specified by:
        run in interface java.lang.Runnable