java - Thread.sleep stops all nested Asyntasks -


i following tutes codelearn, , trying create asynctask generates tweets , executes asynctask write cache file. have thread.sleep, ui on first load waits until tweets written cache file. first execute aysnctask new asyncwritetweets(this.parent).execute(tweets); sleep 10 secs.

but in logcat can see asyncwritetweets gets executed after 10 sec sleep. hence onpostexecute gets executed before tweets written cache file, giving 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");         (int i=0;i<4;i++){             tweet tweet = new tweet();             tweet.settitle("title async new" + i);             tweet.setbody("body text 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: assumption asynctask should create new thread, hence thread.sleep in parent should not stop child. if otherwise please advise how can overcome issue.

this:

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

is wrong, asynctask must executed on ui thread , not worker thread. might use handler , post runnable execute safely.

for reference threading rules:

execute(params...) must invoked on ui thread.

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

another part of above link of interest order of execution, :

starting honeycomb, tasks executed on single thread avoid common application errors caused parallel execution.

so first asynctask must end before next 1 might start, migt bring previous parallel behaviour using executeonexecutor(java.util.concurrent.executor, object[]) thread_pool_executor. still execute must done on ui thread.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -