/** * <p>Convenience method to post a {@link Task} that delivers a {@link * Reply} to a {@link ReplyHandler} to the queue of tasks to be * executed.</p> * * @param reply The reply to return. * @param handler The handler to return to. */ public void deliverReply(final Reply reply, final ReplyHandler handler) { enqueue(new ReplyTask(reply, handler)); }
/** * <p>Handshakes with the internal thread. If this method is called using * the messenger thread, this will deadlock.</p> */ public void sync() { if (Thread.currentThread() == thread) { return; // no need to wait for self } final SyncTask task = new SyncTask(); enqueue(task); task.await(); }
/** * <p>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.</p> * * @return True if content existed and was destroyed. */ public boolean destroy() { boolean done = false; enqueue(Terminate.INSTANCE); if (!destroyed.getAndSet(true)) { sendExecutor.shutdownNow().forEach((Runnable task) -> {((MessageTask) task).msg.discard();}); try { synchronized (this) { while (!queue.isEmpty()) { wait(); } } thread.join(); } catch (final InterruptedException e) { // ignore } done = true; } return done; }