0

I have been working on app that has several activities and services.

In one on my activity, I have starting a thread, which is doing some important work for me in background.

Following is code for thread

OnCreate()
{
    Runnable runnable = new MyRunnableThread();
    MyThread= new Thread(runnable);
    MyThread.start();
}

private class MyRunnableThread implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
              //  Doing some work here ..
                Thread.sleep(2000); // Pause of 2 Second
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                FirebaseCrash.report(e);
            }
        }
    }
}

Now this is how my thread is running..

Now i am stopping this thread using following code

MyThread.Intrrupt();

This will stop my thread to execute ..

In my scenario, sometime when app crashes, these thread keep on running and thus causing misbehave in my app logic..

I want these thread to be stopped immediately in case of app crashes.

Is it the correct way of stopping the thread execution and stop ?

I am using thread in service class also and in state of confusion that there might be multiple threads of same instance running in app at a time because previous was not stopped correctly !!!

Please guide !!!

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
  • Try adding Thread.currentThread().interrupt() before the FirebaseCrash.report(e) line – Sharath kumar Sep 08 '17 at 09:07
  • My app not crashing due to exception in thread – Rajeev Kumar Sep 08 '17 at 09:08
  • But exception will catch all exceptions no – Sharath kumar Sep 08 '17 at 09:10
  • You can not stop the main thread while any other thread are running. (All the child threads born out of main thread.) You can use function Thread.join() to keep the main thread waiting while other thread(s) execute. – Chetan Laddha Sep 08 '17 at 09:15
  • `thread.interrupt()` won't actually interrupt your thread. Instead, it will change a flag in your thread object. You have to keep checking whether that flag is changed within your thread, e.g. `while (isInterrupted()) { // do some action, thread is not interupted yet}`. Now, as soon as you perform `thread.interrupt()` your `while` loop won't execute anymore, which will result your thread to be finished. Afaik, there is no way to explicitly kill a thread in java. – azizbekian Sep 08 '17 at 09:20
  • @azizbekian How it will behave in case of app crashes ? – Rajeev Kumar Sep 08 '17 at 09:22
  • 1
    Thread is not aware about your app crash. It will still continue to execute, unless you handle crash on your own using `Thread.UncaughtExceptionHandler`. – azizbekian Sep 08 '17 at 09:24
  • It might help https://stackoverflow.com/questions/24110619/android-catch-unhandled-exception-and-show-the-dialog – Raghavendra Sep 08 '17 at 09:26
  • How exactly does the app crash? – bowmore Sep 08 '17 at 10:03
  • What's your service's `onStartComand()` return value? – Onik Sep 08 '17 at 17:14
  • @Onik START_NOT_TRICKY – Rajeev Kumar Sep 11 '17 at 06:04

0 Answers0