androidmultithreadingandroid-asynctaskandroid-anr-dialogandroid-runonuithread

Android: 'threadid=3: reacting to signal 3' when executing long task


I am executing a command with Runtime.getRuntime().exec(cmd) inside my app. As it takes long to finish execution(several minutes), I have put it inside a thread. but with thread approach also app is getting suspended halfway and I see this message:

threadid=3: reacting to signal 3
Wrote stack traces to '/data/anr/traces.txt'

following is the code that I am using:

Thread thread = new Thread() {
        public void run() {                
            Process process = Runtime.getRuntime().exec(cmd);
            //cmd : command to be executed
            process.waitFor();

        }
    };
    thread.start();
    thread.join();

What am I missing here? How can I prevent my app from getting suspended due to long processing time?

Note: I know that there are other approaches such as AsyncTask etc. but I don't think it will make any different in this case. (or will it?)


Solution

  • Your app is causing an ANR on this line: thread.join(). That call causes the current thread to wait for the other thread to die before it continues. If this is called on the main thread and the other thread takes longer than 5 seconds to continue, this will cause an ANR. Depending on what you are trying to do, it is best to not call join on the thread and call an observer once the thread is about to finish.

    For an example of how to implement an observer, please see http://stackoverflow.com/questions/994840/how-to-create-our-own-listener-interface-in-android