Class Tasks

java.lang.Object
org.jtrim2.concurrent.Tasks

public final class Tasks extends Object
Defines static methods to return simple, convenient task related instances.

This class cannot be inherited nor instantiated.

Thread safety

Methods of this class are safe to be accessed from multiple threads concurrently.

Synchronization transparency

Methods of this class are synchronization transparent.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Consumer<T>
    Returns a Consumer whose apply method does nothing but returns immediately to the caller.
    static Runnable
    Returns a Runnable whose run() method does nothing but returns immediately to the caller.
    static void
    Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible.
    static void
    Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible.
    static Runnable
    Returns a Runnable which will execute the specified Runnable but will execute the specified Runnable only once.
    static Runnable
    Returns a Runnable which will execute the specified Runnable but will execute the specified Runnable only once, failing on multiple run attempts.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • noOpConsumer

      public static <T> Consumer<T> noOpConsumer()
      Returns a Consumer whose apply method does nothing but returns immediately to the caller.
      Type Parameters:
      T - the type of the objects the returned consumer processes
      Returns:
      a Consumer whose apply method does nothing but returns immediately to the caller. This method never returns null.
    • noOpTask

      public static Runnable noOpTask()
      Returns a Runnable whose run() method does nothing but returns immediately to the caller.
      Returns:
      a Runnable whose run() method does nothing but returns immediately to the caller. This method never returns null.
    • runOnceTask

      public static Runnable runOnceTask(Runnable task)
      Returns a Runnable which will execute the specified Runnable but will execute the specified Runnable only once. The specified task will not be executed more than once even if it is called multiple times concurrently (and is allowed to be called concurrently).

      Calling the run method of the returned Runnable multiple times will only result in a single execution and every other call (not actually executing the specified Runnable) will silently return without doing anything.

      Parameters:
      task - the Runnable to which calls are to be forwarded by the returned Runnable. This method cannot be null.
      Returns:
      the Runnable which will execute the specified Runnable but will execute the specified Runnable only once. This method never returns null.
    • runOnceTaskStrict

      public static Runnable runOnceTaskStrict(Runnable task)
      Returns a Runnable which will execute the specified Runnable but will execute the specified Runnable only once, failing on multiple run attempts. The specified task will not be executed more than once even if it is called multiple times concurrently (and is allowed to be called concurrently).

      Attempting to call the run method of the returned Runnable multiple times will cause an IllegalStateException to be thrown after the first attempt.

      Parameters:
      task - the Runnable to which calls are to be forwarded by the returned Runnable. This method cannot be null.
      Returns:
      the Runnable which will execute the specified Runnable but will execute the specified Runnable only once. This method never returns null.
    • runConcurrently

      public static void runConcurrently(Collection<? extends Runnable> tasks)
      Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible. This method was designed for test codes wanting to test the behaviour of multiple tasks if run concurrently. This method will attempt to start the passed tasks in sync (this does not imply thread-safety guarantees), so that there is a better chance that they actually run concurrently.

      This method will wait until all the specified tasks complete.

      Warning: This method was not designed to give better performance by running the tasks concurrently. Performance of this method is secondary to any other purposes.

      Parameters:
      tasks - the tasks to be run concurrently. Each of the specified tasks will run on a dedicated thread. This argument and its elements cannot be null.
      Throws:
      NullPointerException - thrown if the argument or any of the specified tasks is null
      TaskExecutionException - thrown if any of the tasks thrown an exception. The first exception (in the order they were passed) is the cause of this exception and subsequent exceptions are suppressed (via Throwable.addSuppressed).
    • runConcurrently

      public static void runConcurrently(Runnable... tasks)
      Executes the specified tasks concurrently, each on a separate thread, attempting to execute them as concurrently as possible. This method was designed for test codes wanting to test the behaviour of multiple tasks if run concurrently. This method will attempt to start the passed tasks in sync (this does not imply thread-safety guarantees), so that there is a better chance that they actually run concurrently.

      This method will wait until all the specified tasks complete.

      Warning: This method was not designed to give better performance by running the tasks concurrently. Performance of this method is secondary to any other purposes.

      Parameters:
      tasks - the tasks to be run concurrently. Each of the specified tasks will run on a dedicated thread. This argument and its elements cannot be null.
      Throws:
      NullPointerException - thrown if the argument or any of the specified tasks is null
      TaskExecutionException - thrown if any of the tasks thrown an exception. The first exception (in the order they were passed) is the cause of this exception and subsequent exceptions are suppressed (via Throwable.addSuppressed).