Consider this class, AnimationThread:
class AnimationThread implements Runnable {
public void pause() {
doAnimation = false;
}
public void doStart(){
doAnimation = true;
}
@Override
public void run() {
// TODO Auto-generated method stub
if (doAnimation) {
//my code
}
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
}
}
}
Now I am starting this thread in onCreate of an activity (just showing rough code):
AnimationThread animRunnable = new AnimationThread();
animationThread = new Thread(animRunnable);
animationThread.start();
But run() is getting called just once (I traced a log to confirm that). I just want to know that when I started the thread why run() is not getting called repeatedly with 500 sleep. It is just called once.
That is how it is supposed to be.
A Thread runs by executing its run method (just once). After that it is considered done/dead/finished/completed.
If you want to loop, you have to do it yourself (inside of the run method), or use some ExecutorService to call the Runnable repeatedly.