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
Modifier and TypeClassDescriptionstatic interface
Defines the required interface for tasks to be posted to this worker. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
deliverMessage
(Message msg, MessageHandler handler) Convenience method to post aMessenger.Task
that delivers aMessage
to aMessageHandler
to the queue of tasks to be executed.void
deliverReply
(Reply reply, ReplyHandler handler) Convenience method to post aMessenger.Task
that delivers aReply
to aReplyHandler
to the queue of tasks to be executed.boolean
destroy()
Sets the destroyed flag to true.void
enqueue
(Messenger.Task task) Enqueues the given task in the list of tasks that this worker is to process.void
run()
void
start()
Starts the internal thread.void
sync()
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.Task
that delivers aMessage
to aMessageHandler
to 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.Task
that delivers aReply
to aReplyHandler
to 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()
-