Search code examples
javaandroidcallexecute

Something about one onPostExecute call another onPostExecute


My situation:

First OnPostExecute call function A

within A, new another Second OnPostExecute to call function B

WHY A will finish before B finish first?

For example:

public class ToStart extends otherClass{
    public void execute(){
        Target A = new Target("A");
        A.execute();
    }
    public void print(String target){
        Log.v(LOG_TAG, target + " time: " + System.currentTimeMillis());
        if(target.equals("A")){
             Target B = new Target("B");
             B.execute();
        }
    }
    public class Target extends otherClass {
        private String target;

        public Target(String target) {
            this.target = target;
        }

        protected void onPostExecute (String webData){
            super.onPostExecute(webData);
            printTime(target);
        }

        protected String doInBackground(String... params){
            return super.doInBackground(params);
        }
    }
}

Result:

A time: 1459766772187
B time: 1459766772209

If I want to make B finish first and B has to be called by A, how can i do? Any help will be appreciated! Thanks!


Solution

  • You need to wait for B:

    B.execute().get();