0

I have a timer thread:

Thread updateTimerThread = new Thread(new Runnable()
    {
            @Override
            public void run()
            {
                long updatedTime = 0L;
                long timeInMilliseconds = 0L;
                timeInMilliseconds = SystemClock.uptimeMillis() - startTime;//System.currentTimeMillis() - startTime;
                updatedTime = timeSwapBuff + timeInMilliseconds;
                int secs = (int) (updatedTime / 1000);
                int mins = secs / 60;
                secs = secs % 60;
                int milliseconds = (int) (updatedTime % 1000);
                timerValue.setText("" + mins + ":"
                        + String.format("%02d", secs) + ":"
                        + String.format("%03d", milliseconds));
                customHandler.postDelayed(this, 100);

                //updateTimerThread.start();
            }
    });

I start it like this:

updateTimerThread.start();

Then to stop it i tried:

updateTimerThread.stop();

But i see a black line over the work stop.

stop

My question is if it's fine ? What is this black line over the stop ? Maybe i need to use something else then stop ?

What i need it to do is when i click to stop it it will pause stay on the time it was getting to and when i will click start again it will start the timer over again.

So maybe i don't need stop maybe somehow to pausing it ? And when i make start again it will reset it and will start from the beginning ?

Daniel van wolf
  • 393
  • 1
  • 5
  • 16
  • 1. Declare a boolean run "isRunning" in the thread. 2. Make that variable to "true" before run() method. 3. Have condition in run() method : while (isRunning) 4. Make isRunning as false when you want to stop the thread. – Ravindra babu Sep 14 '15 at 09:04

1 Answers1

1

Just let it exit the run() method once the work you're doing is finished. No need to explicitly stop the thread.

Amila
  • 5,195
  • 1
  • 27
  • 46