Reputation: 8965
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
}
Upvotes: 3
Views: 3170
Reputation: 337
First, I would look into ExecutorService and breaking your code up into a Callable. This would allow you to run it in a thread with the ability to return a value or let exceptions rise. Then, you could restart it if problems occur.
It's a LARGE code smell whenever code catches Exception and / or Throwable. Any exceptions beyond the checked exceptions, should be thrown. If you use the callable pattern like mentioned above, you can check what you're getting, log it, and restart (if necessary).
Upvotes: 0
Reputation: 178411
The problem just screams DeadLock - it shows all the classic symptoms for one.
A deadlock is when thread A is waiting to thread B to finish and free some resource (lock), while thread B is waiting to thread A to finish and free some resource as well.
More information on deadlocks can be found in the java tutorial
This results in both threads stop execution and waiting for nothing.
Finding if a program indeed has a deadlock could be very tricky in some cases, even when using a program that is designed to detect dead-locks, the usage of the program could "solve the problem" temporary. for more details see Heisenbug.
Sometimes (though not always) the best way to detect the dead lock is to look at the abstraction of the program, and try to check all dependecies between threads - and see where a possible collision in dependecies might happen.
P.S. Another common syncrhonzation problem is Data Race
Upvotes: 1
Reputation: 28752
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.
Upvotes: 4
Reputation: 42586
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.
Upvotes: 1