Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Multiple Java Timers...

Posted on 2009-02-16
Medium Priority
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
  • 2
LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 23650866
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

ID: 31547362
Yup That did it! Thanks!!
LVL 86

Expert Comment

ID: 23651659

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

581 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