1

I have an applicaction that comunicates with other devices in two different ways: Sending sound signals and sending messages through Wifi. To Handle the Wifi comunication I created a Service that have a Thread listening for Broadcast messages. For the sounds signals I have as well I thread listening. So in total two threads, that I want to kill when I want byt pressing a button.

In the main Activity I call this method when I press the button:

public void onStop() {
        super.onStop();

        mHelloService.stop();
        //This is the Sounds Thread       
        mListenThread = null;                       

        if(D) Log.e(TAG, "-- ON STOP --");
    }

And in the Service I have this function to stop the Wifi-Thread

public synchronized void stop() {
       System.out.println("Close service");
       if (mHelloThread != null) mHelloThread = null;

    }

I try this, but the threads keep working. I read in another question that someone recommend to use:

mHelloThread.interrupt();

But I tried it and I got these errors:

08-30 20:18:08.592: D/AndroidRuntime(7312): Shutting down VM 08-30 20:18:08.592: W/dalvikvm(7312): threadid=1: thread exiting with uncaught exception (group=0x419bc930) 08-30 20:18:08.592: E/AndroidRuntime(7312): FATAL EXCEPTION: main 08-30 20:18:08.592: E/AndroidRuntime(7312): java.lang.NullPointerException 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.HelloMessage.stop(HelloMessage.java:82) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.SoundLocalizer.onStop(SoundLocalizer.java:349) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.nacho.SoundLocalizer.SoundLocalizer$3.onClick(SoundLocalizer.java:405) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.view.View.performClick(View.java:4202) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.view.View$PerformClick.run(View.java:17340) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Handler.handleCallback(Handler.java:725) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Handler.dispatchMessage(Handler.java:92) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.os.Looper.loop(Looper.java:137) 08-30 20:18:08.592: E/AndroidRuntime(7312): at android.app.ActivityThread.main(ActivityThread.java:5039) 08-30 20:18:08.592: E/AndroidRuntime(7312): at java.lang.reflect.Method.invokeNative(Native Method) 08-30 20:18:08.592: E/AndroidRuntime(7312): at java.lang.reflect.Method.invoke(Method.java:511) 08-30 20:18:08.592: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-30 20:18:08.592: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-30 20:18:08.592: E/AndroidRuntime(7312): at dalvik.system.NativeStart.main(Native Method)

What do you recommend me to do?? Thank you very much

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
  • possible duplicate of [How do you kill a thread in Java?](http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java) – Gray Aug 30 '13 at 18:47
  • Thanks for the feedback, I will read it and see if I can learn something new. But well I asked about Android (I know almost everything from Java can be used in Android), but maybe someone knows a better way to do it that is specially for Android. – Ignacio Alorre Aug 30 '13 at 18:54
  • what's `mHelloService`? An Android `Service` doesn't have a `stop()` method AFAIA... edit: derp nvm, you added it I realise... – ataulm Aug 30 '13 at 22:29

3 Answers3

2

Never use stop(), period.

interrupt() method will set the interrupt flag. You need to implement your own mechanism whereby you periodically check if a thread is interrupted and take appropriate actions.

if(isInterrupted()){
    // do something
}

How do you check if a thread is interrupted ? By using isInterrupted() method.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Ok thanks for the feed back, but now I have two more questions: 1-Why shouldn't I use stop() 2-What should I write insise the if()isInterrupted(){... } should I write there the mHelloThread = null; or are there some other methods to stop the thread? – Ignacio Alorre Aug 30 '13 at 18:49
  • 2
    There is `join()` which can be used to stop the thread. As to why you shouldnt use `stop()`: http://docs.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html – An SO User Aug 30 '13 at 18:53
  • @ataulm there definitely has been an edit to the question sine I posted the answer :) – An SO User Aug 31 '13 at 01:21
  • Is working, thank you very much. And in case now I want it to work back?? what method should I use?? – Ignacio Alorre Sep 01 '13 at 19:55
  • You can not call `start()` on a thread after it has died. For situations where you may wanna restart a thread, consider using `ThreadPool` – An SO User Sep 02 '13 at 00:58
2

Why dont you just change the conditions/variables that keep the thread running.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
1

You're on the right track with setting the Thread to null but you may be misunderstanding what this assignment is doing; it does nothing to the Thread object itself. As Robin said, change the conditions which are keeping the thread running.

Consider the following:

public Something implements Runnable {
    private Thread helloThread;
    public Something() {
        helloThread = new Thread(this);
        helloThread.start();
    }

    @Override
    public void run() {
        while (true && helloThread == Thread.currentThread()) {
            // do something;
        }
    }     
}

When you instantiate Something, it'll create a new worker Thread and call the run() method. It will stay in the while loop until the condition becomes false. If you set helloThread = null, it will become false, and so will return from the run() method and the Thread will stop of its own accord.

ataulm
  • 15,195
  • 7
  • 50
  • 92
  • Thank you very much. I though there was a special method to kill a thread when you need it. I'm not 100% sure, because now it's a long time since I wroked with C++, but I think there was one, and I though maybe in Android/Java there was one as well. Thanks for your time!! – Ignacio Alorre Aug 30 '13 at 23:19
  • There used to be `stop()` but as Little Child stated, it's deprecated because it's not a safe way to kill a Thread. – ataulm Aug 31 '13 at 08:48