-1

I have written a Thread with class Runnable as follows

class myThread implements Runnable {
public void start () {
   thread = new Thread(this, threadName);
   thread.setUncaughtExceptionHandler(handler)
   thread.start()
}
@Override
run(){
 while(toggle) { 
    try {
     //do something
    } catch {
       throw new Runtime Exception()
    }
 }
}

Handler is defined in another class where

new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable exception) {
                toggle = false
                try {
                    thread.start();
                } catch (Exception e) {
                    // Exception in thread
                }
                try {
                    thread.join(200);
                } catch (InterruptedException e) {

                }
                if (!thread.isAlive()){
                   // Tell Everyone Thread is Dead
                } else {
                    System.out.println("Thread is still Alive ");
                }
            }
        });

When I implemented this in Huawei Mobile, it works fine, thread.start, makes the run function reach its end and the thread is dead. but in Samsung tablet even after thread.join in Handler thread is still Alive.

Question: Why is there inconsistance between different devices perfomance, API and Android version is same

Question: What actually happens to thread after Runtime Exception is thrown?

Note: in Samsung Tablet for thread.start excpetion is IllegalThreadException

  • I think the main question here is why do you react to exception thrown from **running** thread by trying to start it again? – M. Prokhorov Mar 28 '18 at 17:10
  • @M.Prokhorov Thats the second question actually: Does, Throwing a Runtime Exception ensure that thread is dead already? because thread.isAlive returns true even after exception thrown. – Jalil Ahmed Siddiqui Mar 28 '18 at 17:22
  • The uncaught exception handler is called right before thread is terminated, which is evidenced by Javadoc of the `UnhaughtExceptionHandler` class. Your code should not rely any particular thread state, the Handler should do what it is intended for: handling exception. Not restarting the thread. not trying to `join` with it (potentially blocking internal housekeeping in the process as well). Not reporting to the outside. So, question stands: why do you need any of this? What XY problem are you actually trying to solve? – M. Prokhorov Mar 28 '18 at 17:39
  • @M.Prokhorov, I am creating a new thread to read from bluetooth sensor. and in case i get an inputStream exception I want my UI to know that thread is dead. Thats why I check for thread state before calling back UI. – Jalil Ahmed Siddiqui Mar 28 '18 at 20:39
  • The thing about this is you have entagled you app with concept that should not bother you. Whether or not a thread is dead, especially on Android, is not a concern of you app. If you want messaging of some sort, then do the messaging, not thread state management. – M. Prokhorov Mar 29 '18 at 11:14

1 Answers1

0

Your code is nonsensical.

  • The start method in your Runnable is pointless. It will never be called by the thread running the Runnable.
  • Calling Thread#start on the thread passed to uncaughtException is guaranteed to result in an IllegalThreadStateException, because the thread must have already been started or it would not have thrown an exception.
StackOverthrow
  • 1,158
  • 11
  • 23
  • I didnt get your first point: myThread.start() is used by main thread to call run() function of thread. it would be good to explain it further.(you can check https://www.tutorialspoint.com/java/java_multithreading.htm for example of my implementation) For Second point: I know it will get illegalThreadStateException but what is the reason for different behaviour of different devices. for one device its working? the question is Why? when it shouldnt – Jalil Ahmed Siddiqui Mar 28 '18 at 17:15