Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread stops with no Exception

When I use 4 threads for my program there is usually no problems, but today I increased it to 8 and I noticed 1-3 threads stop working without throwing any exceptions. Is there anyway to find out why they are stopping? is there anyway to make the thread restart?

This is how the structure of my thread is

public void run()
{
  Main.logger.info(threadName + ": New Thread started (inside run)");
  while (true)
  {
    try
    {
      //all my code
      //all my code
      //all my code
    }
    catch(Exception e)
    {
      Main.logger.error("Exception: " + e);
      try
      {
        Thread.sleep(10000);
      }
      catch (InterruptedException e1)
      {
        e1.printStackTrace();
      }
    }
    finally
    {               
      try
      {
        webClient.closeAllWindows();
        Thread.sleep(3000); 
        Main.logger.info(threadName + ": Closed browser!");
      }
      catch (Exception e)
      {
        Main.logger.error("Exception: " + e);
      }
    }  
  }// end while
}

Regards!

like image 522
Arya Avatar asked Aug 13 '12 01:08

Arya


2 Answers

Note that an Error is not an Exception; it's a Throwable.
So, if you catch Exception, Errors will still get through:

private void m() {    
    try {
        m(); // recursively calling m() will throw a StackOverflowError
    } catch (Exception e) {
        // this block won't get executed, 
        // because StackOverflowError is not an Exception!
    }
}

to catch "everything", change your code to this:

try {
    ...
} catch (Throwable e) {
   // this block will execute when anything "bad" happens
}


Note that there might be little you can do if an Error occurs. Excerpt from javadoc for Error:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

like image 150
Bohemian Avatar answered Nov 15 '22 21:11

Bohemian


Is there anyway to find out why they are stopping?

That's a bit tricky.

A Java thread can terminate for two reasons:

  • it can return from its run() method,
  • it can terminate due to an exception being thrown and not caught on the thread's stack.

You can detect the latter case by using an "UncaughtExceptionHandler" for the thread, but the former case can't be positively detected unless you modify your thread's run() method to log the event ... or something like that.

I guess, the other way to figure out what is going on would be to attach a debugger to the JVM and get it to report the uncaught exception to you.

(I suspect that the reason you are not seeing any exceptions is that your threads' run methods are not catching / logging all exceptions, AND they don't have an uncaught exception handler.)

is there anyway to make the thread restart?

No. There is no way to restart a Thread that has terminated.

like image 24
Stephen C Avatar answered Nov 15 '22 23:11

Stephen C