Class Messenger
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDefines the required interface for tasks to be posted to this worker. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddeliverMessage(Message msg, MessageHandler handler) Convenience method to post aMessenger.Taskthat delivers aMessageto aMessageHandlerto the queue of tasks to be executed.voiddeliverReply(Reply reply, ReplyHandler handler) Convenience method to post aMessenger.Taskthat delivers aReplyto aReplyHandlerto the queue of tasks to be executed.booleandestroy()Sets the destroyed flag to true.voidenqueue(Messenger.Task task) Enqueues the given task in the list of tasks that this worker is to process.voidrun()voidstart()Starts the internal thread.voidsync()Handshakes with the internal thread.
-
Constructor Details
-
Messenger
public Messenger()
-
-
Method Details
-
start
public void start()Starts the internal thread. This must be done AFTER all recurrent tasks have been added.
- See Also:
-
addRecurrentTask(Task)
-
deliverMessage
Convenience method to post a
Messenger.Taskthat delivers aMessageto aMessageHandlerto the queue of tasks to be executed.- Parameters:
msg- The message to send.handler- The handler to send to.
-
deliverReply
Convenience method to post a
Messenger.Taskthat delivers aReplyto aReplyHandlerto the queue of tasks to be executed.- Parameters:
reply- The reply to return.handler- The handler to return to.
-
enqueue
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()
-