Skip to content

Replace AsyncTask.class (deprecated) using java.util.concurrent

Tyler Sizse requested to merge issue_190_async_task into master

Closes #190 (closed); Closes #191 (closed); Replacing the AsyncTask.class and usages using java.util.concurrent;

NOTES:

  • Created a BackgroundTask.class object that represents an Asynchronous Task

  • Created a TaskHelper.class that handles the execution of BackgroundTask.class objects using the java.util.concurrent library. Used to help simplify the creation of an executor, task executions, task rejections, and thread creation. This class is optional when executing tasks (See tag: TaskHandler Is Optional)

  • Created a TaskThread.class that is used for the creation of new Thread.class objects for the internal executor of the TaskHelper.class

  • Replaced all usages of AsyncTask.class with the new BackgroundTask.class and replaced all executions using the new TaskHelper.class

Extend BackgroundTask.class Example:

public class ExampleTask extends BackgroundTask<Integer, String> {

    public ExampleTask() { }

    @Override
    protected String doInBackground() throws Exception {

        // Called on a background Thread, put asynchronous code here
        // This is the only method that is required to be overridden

        // Example of long running code
        for (int i = 1; i <= 100; i++) {

            // Optional: Used to inform the OnProgressListener of any progress
            this.postProgress(i);

            Thread.sleep(100L);

        }

        return "Hello World!";

    }

    @Override
    protected void onProgressUpdate(Integer progress) { 

        // Called on main/UI Thread with the progress of this task, called after postProgress() is called.

    }

    @Override
    protected void onPostExecute(String result) {

        // Called on the main/UI Thread with the result from doInBackground() method

    }

    @Override
    protected void onError(@NonNull Throwable throwable) {
        super.onError(throwable);

        // Called on the main/UI thread, handle errors here

    }

}

Single Task Example Usage:

TaskHandler taskHandler = new TaskHandler();

taskHandler.execute(new BackgroundTask<Integer, String>() {

    @Override
    protected String doInBackground() throws Exception {

        // Called on a background Thread, put asynchronous code here
        // This is the only method that is required to be overridden

        // Example of long running code
        for (int i = 1; i <= 100; i++) {

            // Optional: Used to inform the OnProgressListener of any progress
            this.postProgress(i);

            Thread.sleep(100L);

        }

        return "Hello World!";

    }

    // Override other methods here

});

taskHandler.shutdown();

Single Task Example Alternative Usage:

TaskHandler taskHandler = new TaskHandler();

taskHandler.execute(new ExampleTask());

taskHandler.shutdown();

Multiple Task Example:

TaskHandler taskHandler = new TaskHandler(4);

for (ExampleTask exampleTask : exampleTaskList)

    taskHandler.execute(exampleTask);

taskHandler.shutdown();

TaskHandler.class Is Optional:

ExecutorService executor = Executors.newSingleThreadExecutor();

new ExampleTask().executeOn(executor);

executor.shutdown();

Simplified Version: Simplified version to help understand how this MR works

ExecutorService executor = Executors.newSingleThreadExecutor();

executor.execute(() -> {

    // Put doInBackground/async code here

    new Handler(Looper.getMainLooper()).post(() -> {
          
        // Put onPostExecute code here

    });

}

executor.shutdown();
Edited by Tyler Sizse

Merge request reports