5

In my application I have a button and when it gets clicked I start a new thread and change the text of button. If I press the button again it will start changing its text faster.

I would like to interrupt the thread when the button is pressed in the second time. What's the correct way to do it?

public class TestActivity extends Activity {

 Button btn;
 int i = 0;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     btn = (Button)findViewById(R.id.btn);
     btn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           runThread();
       }
     });
 }

private void runThread() {
    new Thread() {
      public void run() {
          while (i++ < 1000) {
              try {
                  runOnUiThread(new Runnable() {

                      @Override
                      public void run() {
                          btn.setText("#" + i);
                      }
                  });
                  Thread.sleep(300);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
    }.start();
}
user3764893
  • 697
  • 5
  • 16
  • 33

6 Answers6

4

In this case, just keep a reference to your thread and use Thread.interrupt():

private Thread runThread() {

return new Thread() {
  public void run() {
      while (i++ < 1000) {
          try {
              runOnUiThread(new Runnable() {

                  @Override
                  public void run() {
                      btn.setText("#" + i);
                  }
              });
              Thread.sleep(300);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
}

Then:

 btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       if (myThread != null) myThread.interrupt();
       myThread = runThread();
       myThread.start();
   }
 });

Read this post for more info and options:

How to properly stop the Thread in Java?

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
2

In my opinion, the best way would be using a variable to control this.

Something like:

while(i++ < 1000 && keepRunning)

I see that as a good solution because it cant cause unexpected behavior, as you are sure the exactly moment your thread would exit.

extra--

As a suggestion, I also would recommend you to set your thread non-Damon (setDaemon(false)) because it makes layout changes

Also it is a good practice to give thread a name (setName()) to make it easy on debugging.

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
1

Right now you start a new Thread each time you press the button.

Something like this should work.

public class TestActivity extends Activity {

Button btn;
int i = 0;
Thread countThread = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countThread = new Thread() {
    public void run() {
      while (i++ < 1000) {
          try {
              runOnUiThread(new Runnable() {

                  @Override
                  public void run() {
                      btn.setText("#" + i);
                  }
              });
              Thread.sleep(300);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
 }
 btn = (Button)findViewById(R.id.btn);
 btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       runThread();
   }
 });
}

private void runThread() {
  if(countThread != null) {
    if(countThread.isAlive()) {
        countThread.stop();
    } else {
        countThread.start();
    }
  }
}

I only had a text editor so I can't guarantee if this solves your problem.

AKroell
  • 622
  • 4
  • 18
1

You can use thread.interrupt() to interrupt the thread.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

Try this, Just take another variable j and it will handle your code:-

public class TestActivity extends Activity {

     Button btn;
     int i = 0,j=0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         btn = (Button)findViewById(R.id.btn);
         btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                j=1;
               runThread();
           }
         });
     }

    private void runThread() {
        new Thread() {
          public void run() {
              while (i++ < 1000) {
                  try {
                      runOnUiThread(new Runnable() {

                          @Override
                          public void run() {
                              if(j==1){
                              btn.setText("#" + i);
                               j=0;
                               }
                                else
                                    Thread.interrupted();
                          }
                      });
                      Thread.sleep(300);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          }
        }.start();
    }
Laxmeena
  • 780
  • 2
  • 7
  • 28
  • @user3764893, ok just use Thread.interrupted(); inside else part, i have edited my answer,just check it out.... – Laxmeena Jul 09 '14 at 13:07
0

You can use normal Thread in Android (and call interrupt() for your use case) but frameworks provides other better options by providing helper classes around Threads. You can refer to official documentation page for other options.

HandlerThread is preferred option. You can call quitSafely() or quit() for your use case if you go for HandlerThread

Related post:

Why use HandlerThread in Android

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211