User7723337
User7723337

Reputation: 12018

How to stop or destroy a running Thread

I have one thread class which star in onCreate of an activity.

 class MyThread extends Thread
 {
      void run()
      {
           //My code which takes time.
      }
 }

 //-------------------------- To run the thread
 MyThread mThread = new MyThread();
 mThread.start();

On some external events i need to stop/destroy currently running thread.
And Then i create a new copy of MyThread and call start function on the new thread to run it.

But i am not able to destroy/stop the previous running thread.
Is there any API by which we can destroy running thread.

Upvotes: 4

Views: 18409

Answers (5)

Tamim Attafi
Tamim Attafi

Reputation: 2511

Instead of using Thread.interrupt() or any other methods that can throw an exception, you can implement your own stopThread method. In order to do that, you must have a local boolean variable with the name " keepRunning " for example. If you have a for loop or a while loop inside your working code that takes time, you can check for the boolean, if it's false you break the loop and your code won't be running anymore.

class MyThread extends Thread
 {
      boolean keepRunning = true;
      
      void run()
      {
           //inside your for loop
           if (keepRunning) {
           //keep running code which      takes time.
           }
           
           else break;
      }
      
      void stopThread() {
      keepRunning = false;
      }
 }

 //To run the thread
 MyThread mThread = new MyThread();
 mThread.start();
 
 
 //To stop the thread
 mThread.stopThread();

After stopping the thread, you can initialize it again to mThread.

Upvotes: 2

Bhavesh Vadalia
Bhavesh Vadalia

Reputation: 369

You can use AsyncTask insted of Thread, because its known as Painless Threading in android. Once you implement you don't need to bother about Thread Management.

AsyncTask can easily handle and it's very easy for handling ui.

Upvotes: 2

Fishbreath
Fishbreath

Reputation: 407

The Thread.interrupt() method is the accepted way of stopping a running thread, but some implementation details are on you: if your thread is doing IO (or anything else that can throw InterruptedException), it's your responsibility to make sure that you close any resources you have open before you stop your thread.

This page indicates why some of the thread functionality present in earlier versions of Java was deprecated, and why Thread.destroy() was never actually implemented.

Upvotes: 5

5hssba
5hssba

Reputation: 8079

you cannot destroy...only the android will stop the thread when requires.. you cannot stop or destroy it.. instead try like this..

class MyThread extends Thread
 {
  void run()
  {
 while(bool){
       //My code which takes time.
  }
 }
 }

//-------------------------- To run the thread

   MyThread mThread = new MyThread();
   mThread.start();

now when u want to stop the thread... change bool value to false

bool=false;

now your code doesnt run... and you can start new thread...

Upvotes: 6

Hamza Waqas
Hamza Waqas

Reputation: 629

Try something like this..

 class MyThread extends Thread
 {
      void run()
      {
           //My code which takes time.
      }
 }

 //-------------------------- To run the thread
 MyThread mThread = new MyThread();
 mThread.start();

// PERFORM ALL YOUR OPERATIONS HERE..
mThread.destroy();

Upvotes: -1

Related Questions