0

in my app I have a timer and stop/start buttons. Timer works with thread when I click the button my timer starts working. when I click stop and start it seems like my thread works as 2 parallel threads. How to stop the first thread and create the second ? Here is the code

timer = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final boolean cont = mSharedPreferences.getBoolean("continuesMode", false);
                        final boolean statemant = !stopedAlgo && !mUserStop;
                        if(!statemant){
                            timerMinutes = 0;
                            timerHours = 0;
                        }
                        while(statemant){
                            time="";
                            if(timerMinutes>=60){
                                    timerMinutes = 0;
                                    timerHours++;
                            }

                            if(timerHours<10)
                                time = "0" + String.valueOf(timerHours)+":";
                            else
                                time =  String.valueOf(timerHours)+":";
                            if(timerMinutes<10)
                                time += "0" + String.valueOf(timerMinutes);
                            else
                                time +=  String.valueOf(timerMinutes);
                            HeadSense.this.runOnUiThread(new Runnable(){
                                public void run(){
                                    boolean state = mContinuousMode && !stopedAlgo && !mUserStop ;
                                    if(!stopedAlgo && cont){
                                        mTfsValue.setText(time);
                                        timerMinutes++;
                                    }
                                    else{
                                        timerMinutes = 0;
                                        timerHours = 0;
                                    }
                                }

                            });

                            try {
                                Thread.sleep(1*1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }


                    }
                });

                    timer.start();
HK.avdalyan
  • 724
  • 1
  • 6
  • 21

2 Answers2

1

Of my understanding your first Thread (or second) will never stop because of the following line:

  final boolean statemant = !stopedAlgo && !mUserStop;

This is final, and will only be evaluated once and never change. If it evalutes to true when you create the Thread it will stay true and keep running.

ddmps
  • 4,350
  • 1
  • 19
  • 34
0

You need to declare your boolean statemant as volatile here. What this does is give a hint to the compiler that certain optimizations cannot be taken because this variable is modified by external threads.

Also, you would be interested in this - How to stop a java thread gracefully?

Community
  • 1
  • 1
Kanak Sony
  • 1,570
  • 1
  • 21
  • 37