Class TaskMailboxImpl

    • Constructor Detail

      • TaskMailboxImpl

        public TaskMailboxImpl​(@Nonnull
                               Thread taskMailboxThread)
      • TaskMailboxImpl

        @VisibleForTesting
        public TaskMailboxImpl()
    • Method Detail

      • isMailboxThread

        public boolean isMailboxThread()
        Description copied from interface: TaskMailbox
        Check if the current thread is the mailbox thread.

        Read operations will fail if they are called from another thread.

        Specified by:
        isMailboxThread in interface TaskMailbox
        Returns:
        only true if called from the mailbox thread.
      • size

        public int size()
        Description copied from interface: TaskMailbox
        Returns the current number of mails in this mailbox. (This includes mails in the batch not processed yet.)
        Specified by:
        size in interface TaskMailbox
        Returns:
        number of mails in the mailbox.
      • tryTake

        public Optional<Mail> tryTake​(int priority)
        Description copied from interface: TaskMailbox
        Returns an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or an empty optional otherwise.

        Must be called from the mailbox thread (TaskMailbox.isMailboxThread().

        Specified by:
        tryTake in interface TaskMailbox
        Returns:
        an optional with either the oldest mail from the mailbox (head of queue) if the mailbox is not empty or an empty optional otherwise.
      • tryTakeFromBatch

        public Optional<Mail> tryTakeFromBatch()
        Description copied from interface: TaskMailbox
        Returns an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an empty optional otherwise.

        Must be called from the mailbox thread (TaskMailbox.isMailboxThread().

        Note that there is no blocking takeFromBatch as batches can only be created and consumed from the mailbox thread.

        Specified by:
        tryTakeFromBatch in interface TaskMailbox
        Returns:
        an optional with either the oldest mail from the batch (head of queue) if the batch is not empty or an empty optional otherwise.
      • put

        public void put​(@Nonnull
                        Mail mail)
        Description copied from interface: TaskMailbox
        Enqueues the given mail to the mailbox and blocks until there is capacity for a successful put.

        Mails can be added from any thread.

        Specified by:
        put in interface TaskMailbox
        Parameters:
        mail - the mail to enqueue.
      • putFirst

        public void putFirst​(@Nonnull
                             Mail mail)
        Description copied from interface: TaskMailbox
        Adds the given action to the head of the mailbox.

        Mails can be added from any thread.

        Specified by:
        putFirst in interface TaskMailbox
        Parameters:
        mail - the mail to enqueue.
      • drain

        public List<Mail> drain()
        Description copied from interface: TaskMailbox
        Drains the mailbox and returns all mails that were still enqueued.
        Specified by:
        drain in interface TaskMailbox
        Returns:
        list with all mails that where enqueued in the mailbox.
      • close

        @Nonnull
        public List<Mail> close()
        Description copied from interface: TaskMailbox
        Close the mailbox. In this state, all pending and future put operations and all pending and future take operations will throw TaskMailbox.MailboxClosedException. Returns all mails that were still enqueued.
        Specified by:
        close in interface TaskMailbox
        Returns:
        list with all mails that where enqueued in the mailbox at the time of closing.
      • runExclusively

        public void runExclusively​(Runnable runnable)
        Description copied from interface: TaskMailbox
        Runs the given code exclusively on this mailbox. No synchronized operations can be run concurrently to the given runnable (e.g., TaskMailbox.put(Mail) or modifying lifecycle methods).

        Use this methods when you want to atomically execute code that uses different methods (e.g., check for state and then put message if open).

        Specified by:
        runExclusively in interface TaskMailbox
        Parameters:
        runnable - the runnable to execute