3

I am working in java android platform. I am creating a child thread from main thread. I want to stop child thread as my requirments. My child thread has simple function which doesn't have any loop. I want to kill child thread and free resources it is using, whenever I want. I searched for it and I found inturrupt() function. My thread is:

public class childThread implements Runnable{
  public void run() {
     firstFunction();
     secondFunction();
    }
}

Main thread has this code to start above thread:

Thread tChild;
tChild = new Thread(new childThread(), "Child Thread");
tChild.start();

My run() function is calling function like this. How to I use interrupt() in this? Please tell me any other way to kill child thread and deallocating its resources.

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
navalp3
  • 117
  • 3
  • 3
  • 8

5 Answers5

5

With a thread's cooperation, you can stop that thread using any method it supports. Without a thread's cooperation, there is no sane way to stop it. Consider, for example, if the thread holds a lock and has put a shared resource into an invalid state. How can you stop that thread without its cooperation?

You have two choices:

  1. Code your threads so that they don't do work that you don't want them to do. Code them so they terminate themselves when they have no work to do. That way, they won't need some other thread to "reach in" from the outside.

  2. Code your threads so that they provide some way to be told that they should terminate and then they terminate cleanly.

But those are your choices -- it can't work by magic.

Think of a thread doing work like your sister borrowing your car. If you need the car back, you need your sister's cooperation to get it back. You can either arrange is so that she comes back when you need the car by herself or you can arrange is so that you tell her when you need the car and then she comes back. But you can't change the fact that she has to know how to bring the car back or it won't get back.

Threads manipulate process resources and put them into invalid states. They have to repair things before they terminate or the process context will become corrupt.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

As you don't have any loop, where check Thread.interrupted() value, and I assume that your firstFunction(); secondFunction();, do a heavy work, you will have to check in the appropiate points inside functions firstFunction(); secondFunction(); the condition Thread.interrupted(), and finish the method if true. I know that you don't have loops, but conceptually would be something like this,

    Runnable runnable = new Runnable() {
      private int counter;

      public void run() {
        while(true){
          System.out.println(">> Executing " + counter++);

          if(Thread.interrupted()){
            break;
          }
        }

      }
    };

    Thread thread = new Thread(runnable);
    thread.start();

    Thread.sleep(10 * 1000);

    thread.interrupt();
  • Inside `public void run(){firstFunction(); secondFunction();}`. Do the `if(Thread.interrupted()){...}` inside your functions in the points you consider must stop the functions if child thread has been interrupted. –  Jun 15 '13 at 12:14
  • i mean to say.. inside while loop or out of it... if i put inside while loop then it will check for interrupt only once before execution of firstFunction() and secondFunction() and these functions will execute again and again. if I put functions out of while then there is no point of using interrupt(). – navalp3 Jun 15 '13 at 12:18
  • In your case, you mentioned that you don't have loop. My loop was only for conceptual purpose. In your case you would use `public void run(){firstFunction(); secondFunction();}` and you would check `Thread.interrupted()` inside firstFunction() and secondFunction() in the points where you want to check is the thread child has been interrupted for finishing the functions. –  Jun 15 '13 at 12:19
  • In that case interrupr condition will be checked only once at the starting of execution. then there is no point of using interrupt. – navalp3 Jun 15 '13 at 12:23
  • @user2409265 - then you will have to check it in many places through the code. It might be better to revise your current design plan. – Chris Stratton Jun 15 '13 at 12:32
0

Your child thread will need to check Thread.interrupted() on a regular basis and exit if it returns true.

It doesn't matter if your code has loops or not. You just need to choose various points in your code execution path to see whether cancellation is necessary.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • How do I check Thread.interrupted()?? I am not using any loop. – navalp3 Jun 15 '13 at 11:57
  • All we can do here is repeat what the documentation says about interrupted thread, so please see this [link](http://developer.android.com/reference/java/lang/Thread.html#interrupt()) – ja_mesa Jun 15 '13 at 12:16
0

Just call interrupt on tChild object and it will interrupt your child thread

Thread tChild;
tChild = new Thread(new childThread(), "Child Thread");
tChild.start();
//...
tChild.interrupt();
Reda
  • 1,277
  • 1
  • 13
  • 27
  • 1
    yeah it will inturrupt child thread but I cannot check tChild.interruped() every time then how will I stop that thread??? – navalp3 Jun 15 '13 at 12:02
-1

Surround your code with try catch. Catch for InterruptedException then come out . If there are loops, put the catch statement outside the loop / break the loop if the catch is inside.

    Runnable runnable = new Runnable() {  
      public void run() {  
          try {  
               while(true) {  
                    // your code  
               }                   
           }(InterruptedException e) {  
                // It comes out   
           }
}
Rizwan_Khan
  • 313
  • 2
  • 10