Skip to content Skip to sidebar Skip to footer

How To Stop The Timer Thread?

How to stop the Timer thread? I have a timer thread and I would like to stop it. How do I do it? I tried to stop by stopping the thread but it was unsuccessful. Any ideas? Thank

Solution 1:

Given that Thread.stop is deprecated, the only option you have is to add some signalling of your own, like a boolean variable that you set when you want it to stop.

e.g.

booleanstop=false;
publicvoidstartTimer() {
    startTime = System.currentTimeMillis();
    finalHandlerhandler=newHandler();
    task =newThread() {
        @Overridepublicvoidrun() {
            if (stop) {
                return;
            }
            longmillis= System.currentTimeMillis()-startTime;
            longsecs= millis / 1000 % 60; // seconds, 0 - 59longmins= millis / 1000 / 60 % 60; // total seconds / 60, 0 - 59longhours= millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless

            timeString = String.format("%02d:%02d:%02d", hours, mins, secs);
            runOnUiThread(newRunnable() {
                @Overridepublicvoidrun() {
                    mtvtimer.setText(timeString);
                }
            });

           handler.postDelayed(task, 1000);
    }

And from somewhere else, you set stop = true to stop it. (this is a rough example code, just to get the idea through)

Solution 2:

well from the snippet you posted , it is not so clear what you want to achieve. You are passing the task , in the handler to be TimerJob and at the same time , you call task.start() , which means , every time the task is starting , it will attach another task to the handler , as long as with the UiThread. So if i have understood correctly your logic , you could do something like the below example :

//Handler should be created only once , and should have the specified methods//in which he is needed to respond , //for complicated tasks , create a new Specific Handler 
 final Handler handler = new Handler();
 //Defining Thread for example
 Thread task;
 publicvoidstartTimer() {
        startTime = System.currentTimeMillis();
        task =new Thread() {
            @Override
            publicvoidrun() {
                long millis = System.currentTimeMillis()-startTime;
                long secs = millis / 1000 % 60; // seconds, 0 - 59long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 -59long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless

                timeString = String.format("%02d:%02d:%02d", hours, mins, secs);
                runOnUiThread(new Runnable() {
                    @Override
                    publicvoidrun() {
                        mtvtimer.setText(timeString);
                    }
                //repeat the task
                handler.postDelayed(task, 1000);
                });     
            }

        };
        //No Reason To start an unmanaged thread //task.start();//instead passing it to the handler , //and still you can call interrupt on the task reference
        handler.postDelayed(task, 1000);

    }


//To call whenever you have to stop the taskpublicvoidstopHandlerTask()
{
     //your logic here
     handler.removeCallbacks(task);
}

Solution 3:

Seems like this is a job for a timer:

//references for the timer, task and start time.
Timer timer;
TimerTask task;
long startTime;

publicvoidonCreate()
{
    task = new TimerTask() {
        @Override
        publicvoidrun() {
            long millis = System.currentTimeMillis()-startTime;
            long secs = millis / 1000 % 60; // seconds, 0 - 59long mins = millis / 1000 / 60 % 60; // total seconds / 60, 0 -59long hours = millis / 1000 / 60 / 60; // total seconds / 3600, 0 - limitless

            mtvtimer.setText(String.format("%02d:%02d:%02d", hours, mins, secs));
        }
    };
}

//start the timerpublicvoidstartTimer()
{
    timer = new Timer();
    timer.schedule(task, 1000, 1000);// start after 1 second and repeat after every second.
}

//stop the timerpublicvoidstopTimer()
{
    if(timer != null) {
        timer.cancel();
        timer.purge();
        timer = null;
    }
}

Post a Comment for "How To Stop The Timer Thread?"