android - Series of AsyncTask does not go for execution instantaneously -
my mainactivity has 2 views: textview , button. on button click, running asynctask farther creates 10 new asynctasks network operations. every new task creation delayed 1 sec. code is:
public class mainactivity extends actionbaractivity { textview tv; button t; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.textview1); t = (button) findviewbyid(r.id.togglebutton1); t.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { getdata(); } }); } void getdata() { supernetworkasynctask s = new supernetworkasynctask(); s.execute(""); } private class supernetworkasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { for(int i=0;i<10;i++) { { ntask = new networkasynctask(); ntask.execute(""); seek { thread.sleep(1000); } grab (interruptedexception e) { e.printstacktrace(); } } } homecoming ""; } @override protected void onpostexecute(string result) { } } private class networkasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { homecoming string.valueof(system.currenttimemillis()); } @override protected void onpostexecute(string result) { tv.settext(result); } } } i expecting moment first networkasynctask execute method called, start execution. when run it, not find networkasynctask begin execution until command comes out of supernetworkasynctask. there way force execution of networkasynctask thread execute method called?
some clarifications:
why networkasynctask created supernetworkasynctask? because if create networkasynctask in main thread, ui freeze time.
why making 10 object? purpose of networkasynctask read info server @ interval of 1 sec n seconds, here n=10.
part 2: updates after doing tests.
observation 1: fellow brian shared way avoid creating asynctasks in nested way, tried code:
void getdata() { runnable runnable = new runnable() { @override public void run() { (int = 0; <= 10; i++) { ntask = new networkasynctask(); ntask.executeonexecutor(asynctask.thread_pool_executor); seek { thread.sleep(1000); } grab (interruptedexception e) { e.printstacktrace(); } } } }; new thread(runnable).start(); } this freezes ui few seconds , screen updated in fraction of second. quite surprising me too.
observation 2:
with java.lang.thread, experimented create sure 1) threads should executed right away when run() called. 2) next task created after previous task finished.
code:
public static void main(string[] args) { mythread m; (int i=0;i<10;i++) { m=new mythread(string.valueof(i)); m.start(); synchronized (m) { seek { m.wait(); } grab (interruptedexception e) { e.printstacktrace(); } } } } public class mythread extends thread { public string name = ""; public mythread(string n) { name = n; } public void run() { synchronized (this) { system.out.println(" thread name = " + name); seek { thread.sleep(2000); } grab (interruptedexception e) { e.printstacktrace(); } notifyall(); } } } output:
thread name = 0 thread name = 1 thread name = 2 thread name = 3 thread name = 4 thread name = 5 thread name = 6 thread name = 7 thread name = 8 thread name = 9 based in this, updated networkasynctask & supernetworkasynctask as:
private class networkasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { synchronized (this) { homecoming string.valueof(system.currenttimemillis()); } } @override protected void onpostexecute(string result) { synchronized (this) { tv.settext(result); notifyall(); } } } private class supernetworkasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { for(int i=0;i<10;i++) { ntask = new networkasynctask(); ntask.execute(url); synchronized (ntask) { seek { ntask.wait(); } grab (interruptedexception e1) { e1.printstacktrace(); } seek { thread.sleep(2000); } grab (interruptedexception e) { e.printstacktrace(); } } } homecoming ""; } @override protected void onpostexecute(string result) { } } with code wait() keeps on waiting indefinitely.
finally replaced:
ntask.execute(url); with
ntask.executeonexecutor(thread_pool_executor, ""); this worked expected.
the ui updated @ onpostexecute(). see notes on asynctask click here! , seek avoid 10 aysnctasks, not create sense.
android multithreading android-asynctask
No comments:
Post a Comment