Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reproduce AsyncTask from Android in Java pure code

Tags:

java

android

I'm trying to reproduce the same logical that Android's AsyncTask follows in Java pure code.

To do that I have created the abstract class AsyncTask implementing my IAsynTask that basically calls onPreExecute, doInBackground and onPostExecute, also I have created the method execute where the magic happens.

So to use that in my login process, for example, I've created LoginTask class extending AsyncTask, but unlike I had imagined the implemetation of onPreExecute, doInBackground and onPostExecute doesn't look to be mandatory. What am I doing wrong?

Inteface:

public interface IAsyncTask {

    public void onPreExecute();
    public void doInBackground();
    public void onPostExecute();

}

Class:

public abstract class AsyncTask implements IAsyncTask{

    public void onPreExecute() {

    }

    public void doInBackground(){

    }

    public void onPostExecute() {

    }

    public void execute() {

        onPreExecute();

        new Thread(new Runnable() {
            @Override
            public void run() {

                doInBackground();

                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        onPostExecute();
                    }
                });
            }
        }).start();
    }
}

[EDITED]

With my questions about javafx I have noticed that many newer developers are facing problem managing Threads. I would like to share what I have done to simplify my life about managing threads on javafx. I've created an AsyncTask class based on AsyncTask from Android that basically do the same of Android's in a humble but effective way. You can find more information about it on Github project

like image 812
Victor Laerte Avatar asked Jul 11 '13 14:07

Victor Laerte


1 Answers

I'm sorry if I didn't can explain what exactly I wanted, but after you see my code you guys will understand. I just added a abstrac identifier in my methods and got what I wanted. It could seem like a newbie issue, but I think it can be used to simplify some simple operations without directly manage threads.

What I did to reproduce a simple AsyncTask using Java and Javafx code:

create a abstract class AsyncTask

public abstract class AsyncTask {

    abstract void onPreExecute();

    abstract void doInBackground();

    abstract void onPostExecute();

    public void execute() {

        onPreExecute();

        new Thread(new Runnable() {
            @Override
            public void run() {

                doInBackground();

                //Platform.runLater is a javafx code that executes onPost in Main Thread.
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        onPostExecute();
                    }
                });
            }
        }).start();
    }
}

Extend it from our worker class

public class LoginTask extends AsyncTask {

    @Override
    void onPreExecute() {

        //Some code to preexecute in Main Thread
        System.out.println("OnPreExecute - Main Thread: " + Platform.isFxApplicationThread());
    }

    @Override
    void doInBackground() {

        //Some code to execute in background thread as internet requests
        System.out.println("doInBackground - Main Thread: " + Platform.isFxApplicationThread());
    }

    @Override
    void onPostExecute() {

        //Some code to execute in Main thread after background process has done, like update a view
        System.out.println("onPostExecute - Main Thread: " + Platform.isFxApplicationThread());        
    }

}

To call it you can do:

LoginTask taskTest = new LoginTask();
taskTest.execute();

and your log will be:

OnPreExecute - Main Thread: true
doInBackground - Main Thread: false
onPostExecute - Main Thread: true
like image 160
Victor Laerte Avatar answered Nov 12 '22 16:11

Victor Laerte