Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread stops working and no Exception or Throwable is thrown

I usually run my program with about 4 threads and anywhere between 1 hour to 10 hours after the program is running some threads just stop and do nothing, I have my program around Exception and Throwable block and I also have a uncaughtException for the thread and nothing gets added to my log files about any errors. How can I find what is causing the threads to stop?

EDIT

Here is the basic structure of my code

Below is the code for setting the uncaughtException block

                newThread.setName("Thread" + totalNumberOfThreads);
                newThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
                {
                    @Override
                    public void uncaughtException(Thread t, Throwable e)
                    {
                        Main.logger.info("ERROR! An exception occurred in " + t.getName() + ". Cause: " + e.getMessage());
                    }
                });
                newThread.start();

And this is the basic structure of the thread

@Override
public void run()
{               
  while (true)
  {
    try
    {               
      //a long section of code
    }
    catch (Exception e)
    {
      e.printStackTrace();
      Main.logger.info(threadName + ": " + e);
    }
    catch (Throwable t)
    {
      Main.logger.info(threadName + ": " + "Throwable: " + t);
    }
    finally
    {      
      //some code to close connections.. ect..
    }
  } // end while
}
like image 834
Arya Avatar asked Jul 23 '26 02:07

Arya


2 Answers

How can I find what is causing the threads to stop?

Do you know for a fact that the threads have stopped? As mentioned they might be blocked. You can use either jstack to see if they are active or for closer inspection start the program with remote debugging enabled and attach a debugger when you feel they have stopped.

like image 79
Miserable Variable Avatar answered Jul 25 '26 16:07

Miserable Variable


You could try running the program in a debugger, which would let you inspect the state of the threads.

You can also use the built-in Ctrl-Break or Ctrl-\mechanism in Java - see Troubleshooting Hanging or Looping Processes, which will cause the HotSpot VM to print a thread dump, including thread state.

like image 44
DNA Avatar answered Jul 25 '26 17:07

DNA