It's not a secret anymore!
The moose likes Java in General and the fly likes Java util Timer terminates itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java util Timer terminates itself" Watch "Java util Timer terminates itself" New topic

Java util Timer terminates itself

sram bahl

Joined: Nov 26, 2002
Posts: 13
I'm using java.util.Timer class to monitor some of my application process and report to a controller. This monitor runs 24 X 7 at a fixed polling interval. What i notice is after 5-6 days the monitor terminates itself. My actual code has two monitors (SelfMonitor and AppMonitor), both running at different intervals. What could be the problem. If i add a shutdownhook is it possible to ascertain what caused the termination ? I do not call cancel anywhere on my code.

public class TimerProblem {

class SelfMonitor extends TimerTask {
* Send SaJvmMonitor heartbeat to the controller every 10 mins.
public void run() {"SaJvmMonitor is Alive...");
try {
conn = (HttpURLConnection) controlUrl.openConnection();
PrintWriter out = new PrintWriter(conn.getOutputStream());
log.debug("connected to controller " + conn);
"SaJvm Status = "
+ URLEncoder.encode("SaJvmMonitor is Alive..."));
int code = conn.getResponseCode();
} catch (Exception ex) {
"Unable to establish connection with the Controller "
+ controller);;
} finally {
out = null; //release PrintWriter for GC


public static void main(String[] ar) {
timer = new Timer();
timer.schedule(new AppMonitor(), 0, pulseRate * 60 * 1000);
timer.schedule(new SelfMonitor(), 0, pulseRate * 3 * 60 * 1000);
I agree. Here's the link:
subject: Java util Timer terminates itself
It's not a secret anymore!