de.caff.util.concurrent

## Class PriorityThreadPoolExecutor

- java.lang.Object
- - de.caff.util.concurrent.PriorityThreadPoolExecutor

- All Implemented Interfaces:
    PriorityExecutor

      public class PriorityThreadPoolExecutor
      extends java.lang.Object
      implements PriorityExecutor

  Same as thread pool executor, but supporting priorities.

  Added tasks of the same priority are run in order.

  Author:
    Rammi

- - ### Field Summary

    - ### Fields inherited from interface de.caff.util.concurrent.PriorityExecutor

      `CANT_REMOVE, DIRECT_EXECUTOR, NO_ID`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit)` Constructor.
    `PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)` Constructor.
    `PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)` Constructor.
    `PriorityThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `long` | `execute(Priority priority, java.lang.Object mark, java.lang.Runnable task)` Execute a task with the given priority.
    `long` | `executeWithLifeCycle(Priority priority, TaskLifeCycleListener mark, java.lang.Runnable task)` Execute a task with the given priority.
    `int` | `removeTasksWithMark(java.lang.Object mark)` Remove all tasks which where started with the given mark.
    `boolean` | `removeTaskWithId(long id)` Remove the task with the given id.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### PriorityThreadPoolExecutor

          public PriorityThreadPoolExecutor(int corePoolSize,
                                            int maximumPoolSize,
                                            long keepAliveTime,
                                            @NotNull
                                            java.util.concurrent.TimeUnit unit)

      Constructor.
      Parameters:
        `corePoolSize` - core pool size
        `maximumPoolSize` - maximum pool size
        `keepAliveTime` - keep alive time
        `unit` - keep alive time unit
      See Also:
        `ThreadPoolExecutor.ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue)`

    - #### PriorityThreadPoolExecutor

          public PriorityThreadPoolExecutor(int corePoolSize,
                                            int maximumPoolSize,
                                            long keepAliveTime,
                                            @NotNull
                                            java.util.concurrent.TimeUnit unit,
                                            @NotNull
                                            java.util.concurrent.ThreadFactory threadFactory)

      Constructor.
      Parameters:
        `corePoolSize` - core pool size
        `maximumPoolSize` - maximum pool size
        `keepAliveTime` - keep alive time
        `unit` - keep alive time unit
        `threadFactory` - thread creation factory
      See Also:
        `ThreadPoolExecutor.ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory)`

    - #### PriorityThreadPoolExecutor

          public PriorityThreadPoolExecutor(int corePoolSize,
                                            int maximumPoolSize,
                                            long keepAliveTime,
                                            @NotNull
                                            java.util.concurrent.TimeUnit unit,
                                            @NotNull
                                            java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)

      Constructor.
      Parameters:
        `corePoolSize` - core pool size
        `maximumPoolSize` - maximum pool size
        `keepAliveTime` - keep alive time
        `unit` - keep alive time unit
        `rejectedExecutionHandler` - rejected execution handler
      See Also:
        `ThreadPoolExecutor.ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)`

    - #### PriorityThreadPoolExecutor

          public PriorityThreadPoolExecutor(int corePoolSize,
                                            int maximumPoolSize,
                                            long keepAliveTime,
                                            @NotNull
                                            java.util.concurrent.TimeUnit unit,
                                            @NotNull
                                            java.util.concurrent.ThreadFactory threadFactory,
                                            @NotNull
                                            java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)

      Constructor.
      Parameters:
        `corePoolSize` - core pool size
        `maximumPoolSize` - maximum pool size
        `keepAliveTime` - keep alive time
        `unit` - keep alive time unit
        `threadFactory` - thread creation factory
        `rejectedExecutionHandler` - rejected execution handler
      See Also:
        `ThreadPoolExecutor.ThreadPoolExecutor(int, int, long, java.util.concurrent.TimeUnit, java.util.concurrent.BlockingQueue, java.util.concurrent.ThreadFactory, java.util.concurrent.RejectedExecutionHandler)`

  - ### Method Detail

    - #### execute

          public long execute(@NotNull
                              Priority priority,
                              @Nullable
                              java.lang.Object mark,
                              @NotNull
                              java.lang.Runnable task)

      Execute a task with the given priority.
      Specified by:
        `execute` in interface `PriorityExecutor`
      Parameters:
        `priority` - priority of task
        `mark` - mark which allows access to objects in queue. Not all implementation need to take care of this.
        `task` - task to run
      Returns:
        an id allowing later access to this task
      See Also:
        `removeTasksWithMark(Object)`

    - #### executeWithLifeCycle

          public long executeWithLifeCycle(@NotNull
                                           Priority priority,
                                           @NotNull
                                           TaskLifeCycleListener mark,
                                           @NotNull
                                           java.lang.Runnable task)

      Execute a task with the given priority.
      Specified by:
        `executeWithLifeCycle` in interface `PriorityExecutor`
      Parameters:
        `priority` - priority of task
        `mark` - mark which allows access to objects in queue, and also gets informed when the task is enqueued, started and finished. Not all implementation need to take care of this.
        `task` - task to run
      Returns:
        an id allowing later access to this task, or `PriorityExecutor.NO_ID` if access is no longer possible

    - #### removeTasksWithMark

          public int removeTasksWithMark(@NotNull
                                         java.lang.Object mark)

      Remove all tasks which where started with the given mark.
      Specified by:
        `removeTasksWithMark` in interface `PriorityExecutor`
      Parameters:
        `mark` - mark to check for
      Returns:
        number of removed tasks if removing was possible, or `PriorityExecutor.CANT_REMOVE` if the executor does not allow removing

    - #### removeTaskWithId

          public boolean removeTaskWithId(long id)

      Remove the task with the given id.
      Specified by:
        `removeTaskWithId` in interface `PriorityExecutor`
      Parameters:
        `id` - id
      Returns:
        `true` if a rask with the given id was found and removed
        `false`: if none was found