Pramoth
Pramoth

Reputation: 51

I need to stop thread when progress bar reaches 0 in android

I need stop thread and handler when my progress bar reaches 0 from 100 when thread runs the progress bar reaches but the progressStatus value going in negative please help me to stop thread after progress bar reaches 0

 new Thread(runn =new Runnable() {
     public void run() {

         while (progressStatus <= 100) {
             progressStatus += doWork();
             try {
                 Thread.sleep(10);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }

             // Update the progress bar
             handler.post(runn1=new Runnable() {
                 public void run() {
                     bar.setProgress(progressStatus);
                     i=-1;                                                  
                     if(bar.getProgress()==0)
                     {
                        handler.removeCallbacks(runn); 
                        handler.removeCallbacks(runn1);
                         System.out.println("Reached");
                      congrats.setVisibility(View.VISIBLE);
                         restart.setVisibility(View.VISIBLE); 
                         rightbutton.setVisibility(View.GONE);
                         wrongbutton.setVisibility(View.GONE);

                     }
                 }
             });



         }



     }
     private int doWork() {

         return i;
      }

     }).start();      

Upvotes: 0

Views: 279

Answers (1)

mmlooloo
mmlooloo

Reputation: 18977

your program is not thread safe, you actually reading and writing a variable (progressStatus) from two different threads, you must avoid doing that or if you want to do that you must use synchronized block. In order to solve your problem you can do this way:

Thread t;
progressStatus = 100;
t =  new Thread(runn =new Runnable() {
     public void run() {

         while (!Thread.currentThread().isInterrupted()) {
             try {
                 Thread.sleep(10);
             } catch (InterruptedException e) {
                 e.printStackTrace();
                 return;
             }
             // Update the progress bar
             handler.post(runn1=new Runnable() {
                 public void run() {
                     bar.setProgress(progressStatus);
                     progressStatus=progressStatus-1;                                                  
                     if(bar.getProgress()==0)
                     {
                        handler.removeCallbacks(runn); 
                        handler.removeCallbacks(runn1);
                        System.out.println("Reached");
                        congrats.setVisibility(View.VISIBLE);
                        restart.setVisibility(View.VISIBLE); 
                        rightbutton.setVisibility(View.GONE);
                        wrongbutton.setVisibility(View.GONE);
                        t.interrupt();

                     }
                 }
             });

another way that i recommend you is using ScheduledThreadPoolExecutor with the function scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit). something like:

final ScheduledThreadPoolExecutor myTimer = new ScheduledThreadPoolExecutor(1);
myTimer.scheduleAtFixedRate(new Runnable() {

                    @Override    
                    public void run() {
                        getActivity().runOnUiThread(new Runnable(){
                            @Override
                            public void run(){
                            }
                       });  


                    }   
            }

}, 0,10, TimeUnit.MILLISECONDS);

and in order to close it use myTimer.shutdownNow();

Upvotes: 1

Related Questions