I created an asynctask and in its doInBackground() method i started a thread like this:
private class myAsyntask extends Asynctask{
doInBackground(){
Thread t = new Thread(new Runnable(){
public void run()
{
while(someBoolean!=true){
Thread.currentThread.sleep(100);
}
}
});
}
onPostExecute(){
//do something related to that variable
}
}
problem I am facing is after 1st iteration of Thread.sleep() , onPostExecute() is called , instead I thought that asynctask will run this thread on background and when that boolean is true onPostexecute() is called.I am not able to understand why this happens ?
AsyncTask automatically creates a new Thread for you, so everything you do in
doInBackground()is on another thread.What you are doing is this:
doInBackground().t) is created from the AsyncTask-Thread.doInBackground()is completed, as all it does is create the Thread t and thus jumps toonPostExecute().start()ont, meaning that it is not started).Instead you want your
doInBackground()method to look something like this: