Multiple Java Timers...

Posted on 2009-02-16
Last Modified: 2012-05-06
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?
Question by:Maarvaadi
    LVL 86

    Accepted Solution

    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


    Author Closing Comment

    Yup That did it! Thanks!!
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    Title # Comments Views Activity
    nested interface 5 29
    how to use external config file with Spring MVC 4 36
    countAbc challenge 9 35
    stringclean challenge 26 43
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now