Class AsyncTask

java.lang.Object
com.codename1.backend.AsyncTask
All Implemented Interfaces:
Runnable, Future

public abstract class AsyncTask extends Object implements Runnable, Future

One call of an @Async method, and the Future its caller holds.

The build generates a subclass per @Async method whose fields are the call's arguments and whose call() invokes the method's original body. The rewritten method constructs one, hands it to its executor and returns it, so the caller's Future is this object. A method whose body itself returns a Future -- AsyncResult.of(V) -- completes this one with that Future's value.

The caller's span, if it was being traced, is the parent of the span the call runs in, so the background work shows up under the request that started it.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AsyncTask(String name, boolean returnsVoid)
    Parameters
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract Object
    Runs the method's body.
    boolean
    cancel(boolean mayInterruptIfRunning)
    Cancels the call if it has not started.
    get()
    Waits for the work to finish and returns its result.
    get(long timeout, TimeUnit unit)
    Waits up to timeout for the work to finish and returns its result.
    boolean
    Whether Future.cancel(boolean) cancelled the work before it ran.
    boolean
    Whether the work has finished: completed, failed or cancelled.
    final void
    run()
    When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
    Returns a string representation of the object.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • AsyncTask

      protected AsyncTask(String name, boolean returnsVoid)
      Parameters
      • name: what the span is called: the class and the method

      • returnsVoid: @param returnsVoid whether the method returns nothing, so a failure has nobody to be delivered to and is reported instead

  • Method Details

    • call

      protected abstract Object call() throws Exception
      Runs the method's body. Generated.
      Throws:
      Exception
    • run

      public final void run()
      Description copied from interface: Runnable
      When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread. The general contract of the method run is that it may take any action whatsoever.
      Specified by:
      run in interface Runnable
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Cancels the call if it has not started. A running call is never interrupted.
      Specified by:
      cancel in interface Future
      Parameters:
      mayInterruptIfRunning - whether a running task may be interrupted
      Returns:
      true when this call cancelled it
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Future
      Whether Future.cancel(boolean) cancelled the work before it ran.
      Specified by:
      isCancelled in interface Future
      Returns:
      true when cancelled
    • isDone

      public boolean isDone()
      Description copied from interface: Future
      Whether the work has finished: completed, failed or cancelled.
      Specified by:
      isDone in interface Future
      Returns:
      true once it will not change again
    • get

      Description copied from interface: Future
      Waits for the work to finish and returns its result.
      Specified by:
      get in interface Future
      Returns:
      the result
      Throws:
      InterruptedException - when the waiting thread is interrupted
      ExecutionException - when the work threw; Throwable.getCause() is what it threw
    • get

      public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Description copied from interface: Future
      Waits up to timeout for the work to finish and returns its result.
      Specified by:
      get in interface Future
      Parameters:
      timeout - how long to wait, in unit
      unit - the unit of timeout
      Returns:
      the result
      Throws:
      InterruptedException - when the waiting thread is interrupted
      ExecutionException - when the work threw
      TimeoutException - when it has not finished in time
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object