Class ExecutorService

java.lang.Object
com.gmt2001.util.concurrent.ExecutorService

public final class ExecutorService extends Object
Provides an interface to a shared ScheduledExecutorService
Author:
gmt2001
  • Method Details

    • executorService

      public static ScheduledExecutorService executorService()
      Returns:
      the backing ScheduledExecutorService
    • schedule

      public static <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
      Creates and executes a ScheduledFuture that becomes enabled after the given delay
      Type Parameters:
      V - the return type of the callable
      Parameters:
      callable - the function to execute
      delay - the time from now to delay execution
      unit - the time unit of the delay parameter
      Returns:
      a ScheduledFuture that can be used to extract result or cancel
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if callable is null
    • schedule

      public static ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
      Creates and executes a one-shot action that becomes enabled after the given delay
      Parameters:
      command - the task to execute
      delay - the time from now to delay execution
      unit - the time unit of the delay parameter
      Returns:
      a ScheduledFuture representing pending completion of the task and whose Future.get() method will return null upon completion
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if command is null
    • scheduleAtFixedRate

      public static ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
      Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay + period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute
      Parameters:
      command - the task to execute
      initialDelay - the time to delay first execution
      period - the period between successive executions
      unit - the time unit of the initialDelay and period parameters
      Returns:
      a ScheduledFuture representing pending completion of the task, and whose Future.get() method will throw an exception upon cancellation
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if command is null
      IllegalArgumentException - if period less than or equal to zero
    • scheduleWithFixedDelay

      public static ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
      Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor
      Parameters:
      command - the task to execute
      initialDelay - the time to delay first execution
      delay - the delay between the termination of one execution and the commencement of the next
      unit - the time unit of the initialDelay and delay parameters
      Returns:
      a ScheduledFuture representing pending completion of the task, and whose Future.get() method will throw an exception upon cancellation
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if command is null
      IllegalArgumentException - if delay less than or equal to zero
    • execute

      public static void execute(Runnable command)
      Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation
      Parameters:
      command - the runnable task
      Throws:
      RejectedExecutionException - if this task cannot be accepted for execution
      NullPointerException - if command is null
    • submit

      public static Future<?> submit(Runnable task)
      Submits a Runnable task for execution and returns a Future representing that task, whose Future.get() method will return null upon successful completion
      Parameters:
      task - the task to submit
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if the task is null
    • submit

      public static <T> Future<T> submit(Runnable task, T result)
      Submits a Runnable task for execution and returns a Future representing that task, whose Future.get() method will return the given result upon successful completion
      Type Parameters:
      T - the return type of result
      Parameters:
      task - the task to submit
      result - the result to return
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if the task is null
    • submit

      public static <T> Future<T> submit(Callable<T> task)
      Submits a value-returning task for execution and returns a Future representing the pending results of the task, whose Future.get() method will return the task's result upon successful completion

      If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();

      Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted

      Type Parameters:
      T - the return type of the callable
      Parameters:
      task - the task to submit
      Returns:
      a Future representing pending completion of the task
      Throws:
      RejectedExecutionException - if the task cannot be scheduled for execution
      NullPointerException - if the task is null
    • shutdown

      public static void shutdown()
      Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down
      Throws:
      SecurityException - if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not hold RuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.
    • isShutdown

      public static boolean isShutdown()
      Indicates if this ExecutorService is shutdown or in the process of shutting down
      Returns:
      true if shutdown