Class CommonTaskExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Executor, java.util.concurrent.ExecutorService

    public final class CommonTaskExecutor
    extends java.util.concurrent.AbstractExecutorService
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean awaitTermination​(long timeout, java.util.concurrent.TimeUnit unit)  
      void execute​(java.lang.Runnable command)  
      boolean isShutdown()  
      boolean isTerminated()  
      <T> java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(CommonTaskExecutor.Task<T> task, T target, long initialDelay, long period, java.util.concurrent.TimeUnit unit, java.lang.String name)
      Run task periodically providing it with target
      void shutdown()  
      java.util.List<java.lang.Runnable> shutdownNow()  
      • Methods inherited from class java.util.concurrent.AbstractExecutorService

        invokeAll, invokeAll, invokeAny, invokeAny, submit, submit, submit
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • scheduleAtFixedRate

        public <T> java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate​(CommonTaskExecutor.Task<T> task,
                                                                               T target,
                                                                               long initialDelay,
                                                                               long period,
                                                                               java.util.concurrent.TimeUnit unit,
                                                                               java.lang.String name)
        Run task periodically providing it with target

        Important implementation detail here is that internally we do not hold any strong references to target which means it can be GCed even while periodic task is still scheduled.

        If target is GCed periodic task is canceled.

        This method should be able to schedule task in majority of cases. The only reasonable case when this would fail is when task is being scheduled during JVM shutdown. In this case this method will return 'fake' future that can still be canceled to avoid confusing callers.

        Parameters:
        task - task to run. Important: must not hold any strong references to target (or anything else non static)
        target - target object to pass to task
        initialDelay - initialDelay, see ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
        period - period, see ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
        unit - unit, see ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
        name - name to use in logs when task cannot be scheduled
        Returns:
        future that can be canceled
      • shutdown

        public void shutdown()
      • shutdownNow

        public java.util.List<java.lang.Runnable> shutdownNow()
      • isShutdown

        public boolean isShutdown()
      • isTerminated

        public boolean isTerminated()
      • awaitTermination

        public boolean awaitTermination​(long timeout,
                                        java.util.concurrent.TimeUnit unit)
                                 throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • execute

        public void execute​(java.lang.Runnable command)