1

While learning using Threads in Android I've created simple thread that updates time textview every second:

Thread t = new Thread() {
        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(time!=0){
                                if(time>9){timeLeftTV.setText("0:"+time);}
                                else{timeLeftTV.setText("0:0"+time);}
                                time--;
                            }
                            else {
                                //timeLeftTV.setText("finished");
                            }
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };
    t.start();

I want to display dialog box when the time expires. How do I stop this thread?

Godfryd
  • 479
  • 1
  • 7
  • 17

4 Answers4

1

use CountDownTimer

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
}.start();

This is example of 30 seconds for 1 second time interval.

You can display dialog box on onFinish() method.

Akash Patel
  • 2,757
  • 1
  • 22
  • 30
1

Most of the time I use a Runnable that can be scheduled with a Handler as followed:

final int timeInterval = 1000;
final Handler handler = new Handler();
Runnable runnable = new Runnable() {

  @Override
  public void run () {

    textView.setText("time..");

    // schedule the same Runnable after one second
    handler.postDelayed(this, timeInterval); 
  }
};
handler.postDelayed(runnable, timeInterval);

To stop your loop, remove the Runnable from the Handler:

handler.removeCallbacks(runnable);

When you don't want to use the method above, simply use a Boolean that prevents your loop to continue and your Thread will end itself:

boolean stop = false;
Thread t = new Thread() {

  @Override
  public void run () {

    while (!stop) {
      // do stuff
    }
  }
};
t.start();

To stop the Thread:

stop = true;
DroidBender
  • 7,762
  • 4
  • 28
  • 37
0

Just interrupt the thread, Where you want to stop it.

thread.interrupt();
Ranjan
  • 1,326
  • 18
  • 38
0

There are many ways to stop thread. Like you can use Executor Services instead of timer. But for the quick solution you can go ahead with the following one:

long startTimer= System.currentTimeMillis(); long stopTimer= startTimer+ 60*1000; // 60 seconds * 1000 ms/sec

while (System.currentTimeMillis() < stopTimer)
{
    // Perform your all the required operation here
}

Hope it will help you.

For Executor service check the below stack link:

How to timeout a thread

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47