android - why countdown counter with thread show wrong value? -
i don't know why count down counter shows random number @ end?
mean shows 60:15, 60:07, on way
min=sec=0; new thread(new runnable() { @override public void run() { while (min < 60 && flagtime) { try { thread.sleep(1); g.handler.post(new runnable() { @override public void run() { string presec=""; string premin=""; if (sec < 59) { sec += 1; } if (sec < 10) { presec = "0"; } if (min < 10) { premin = "0"; } score =premin + min + ":" + presec + sec; txt[elementnumber + 1].settext(score); } }); } catch (exception e) { e.printstacktrace(); } } } }).start();
please tell me why works weird?
timing in oses not precise, unless use framework or tools designed task. can work thread.sleep reasonable uncertainty. "reasonable" , "precise" timing, depends on problem trying solve.
in threads, sleep(1000) not mean thread sleep 1 second, thread sleep less or more every time run app. why random results.
this has many things else consider priority of thread. better way count down use other ways provided android:
you may check on google , find many examples how use them.
those more reliable , more precise.
hope helps.