Thread.sleep stops all nested Asyntasks

I am following tutes from codelearn and trying to create an AsyncTask that generates tweets and executes another AsyncTask to write to the cache file. I have Thread.sleep, so the user interface, on first boot, waits until tweets are written to the cache file. First I execute AysncTask new AsyncWriteTweets(this.parent).execute(tweets);, then sleep for 10 seconds.

But in logcat, I see that AsyncWriteTweets also starts after 10 seconds of sleep. Therefore, onPostExecute is executed before the tweets are written to the cache file, giving a blank screen.

public class AsyncFetchTweets extends AsyncTask<Void, Void, Void> {
    private TweetListActivity parent;
    ArrayList<Tweet> tweets = new ArrayList<Tweet>();
    ArrayList[] temp;

    public AsyncFetchTweets(TweetListActivity parent){
        this.parent = parent;
    }

    @Override
    protected Void doInBackground(Void... params) {
        int result = 0;
        Log.d("ASync", "Calling asycn");
        for (int i=0;i<4;i++){
            Tweet tweet = new Tweet();
            tweet.setTitle("Title Async Very New" + i);
            tweet.setBody("Body text for tweet no " + i);
            tweets.add(tweet);
        }
        new AsyncWriteTweets(this.parent).execute(tweets);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Void result){
        Log.d("Async", "on Post execute");
        this.parent.renderTweets();
    }

}

PS: My guess: AsyncTask should create a new thread, therefore Thread.sleep in the parent should not stop the children. If it is otherwise please advise how I can solve this problem.

+4
2

:

new AsyncWriteTweets(this.parent).execute(tweets);

, AsyncTask , Worker. Handler post runnable .

Threading:

execute (Params...) .

http://developer.android.com/reference/android/os/AsyncTask.html

Order of execution,:

HONEYCOMB, , , .

, , executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.. execute .

+2

execute() . , async, async.

executeOnExecutor .

+1

All Articles