Multiple Java Timers...

I have a need for a timer in my program which will be triggered at every n hours. Now because of a known issue which may happen once in a while there will be a stack overflow in that timer thread, causing that thread to crash. This also means that my timer is now no longer there..

What is the best way to prevent this? I know the stack overflow needs to be fixed, that is being worked on, however in the meantime, all I can think is another timer keeping track of the first timer.. I have had issues in the past when I have 2 timers then one of them fails to fire very quickly (no stack overflows then).. Pointers or suggestions?
Who is Participating?
Just make sure you don't run the task in the main thread. See the following - it doesn't crash the timer:
import java.util.Timer;
import java.util.TimerTask;
public class TimCrash extends TimerTask {
    private int intVal;
    public TimCrash(int intVal) {
	this.intVal = intVal;
    public static void main(String[] args) {
	Timer t = new Timer();
	// Pass zero to main
	t.schedule(new TimCrash(Integer.parseInt(args[0])), 0, 5000);
    public void run() {
	// crash it
	new Thread() {
	    public void run() {
		// Deliberately crash the thread
		int i = 100 / intVal;

Open in new window

MaarvaadiAuthor Commented:
Yup That did it! Thanks!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.