Why is run () called only once?

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 start this thread in onCreateactivity (just showing the rough code):

AnimationThread animRunnable = new AnimationThread();
animationThread = new Thread(animRunnable);
animationThread.start();

But it run()is called only once (I traced the log to confirm this). I just want to know that when I started the topic, why is run()n’t it being called repeatedly with 500 sleep. It just gets called once.

+5
source share
3 answers

This is how it should be.

A thread is started by executing its start method (only once). After that, it is considered completed / dead / completed / completed.

, ( run) ExecutorService Runnable.

+12

, run() . run, . TimerTask - .

EDIT:

+8

run()- a method that is executed only once by any Thread. Because after the end of run methodThread dead or completed.
Thus, only one is executed for one thread. If you do not want to kill the thread, then you must make sure that the execution is complete ...

0
source

All Articles