Class TimedSynchronousQueue<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.SynchronousQueue<E>
eu.cloudnetservice.driver.impl.network.scheduler.TimedSynchronousQueue<E>
Type Parameters:
E - the type of elements held in this queue
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>

final class TimedSynchronousQueue<E> extends SynchronousQueue<E>
An implementation of a synchronous queue that always offers tasks to itself with a timeout instead of returning instantly if that is not possible. The queue also makes some more assumptions when the parent executor service is provided to achieve the maximum scaling of the parent thread pool.
Since:
4.0
  • Field Details

  • Constructor Details

    • TimedSynchronousQueue

      public TimedSynchronousQueue(@NonNull @NonNull LongSupplier timeoutSupplier)
      Constructs a new instance of this queue using the given timeout supplier.
      Parameters:
      timeoutSupplier - the supplier for timeouts used when actually needing to queue elements.
      Throws:
      NullPointerException - if the given timeout supplier is null.
  • Method Details

    • parentExecutor

      public void parentExecutor(@NonNull @NonNull ThreadPoolExecutor parentExecutor)
      Sets the parent thread pool executor that is associated with the queue unless another thread pool executor is already associated with it.
      Parameters:
      parentExecutor - the parent executor to use for this queue.
      Throws:
      NullPointerException - if the given parent executor is null.
      IllegalStateException - if a parent executor was already set.
    • offer

      public boolean offer(@NonNull E element)
      Tries to offer the given element into this queue with a timeout provided by the owner of this queue. If the queue has information about the associated thread pool this method makes some additional considerations to ensure scaling the thread pool to its maximum capacity first.
      Specified by:
      offer in interface BlockingQueue<E>
      Specified by:
      offer in interface Queue<E>
      Overrides:
      offer in class SynchronousQueue<E>
      Parameters:
      element - the element to add into this queue.
      Returns:
      true if the element was added into this queue, false otherwise.
      Throws:
      NullPointerException - if the given element is null.
    • offerSafe

      private boolean offerSafe(@NonNull E element, long timeoutMillis)
      Tries to offer the given element into this queue with the given timeout millis.
      Parameters:
      element - the element to offer into this queue.
      timeoutMillis - the timeout in milliseconds of the offer operation.
      Returns:
      true if some other thread polled the element within the timeout millis, false otherwise.
      Throws:
      NullPointerException - if the given element is null.