Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to make java.util.Timer a daemon

Tags:

java

I have the following java.util.Timer but it does not seem to execute and I don't know why.

public static void main(String[] args)
{
  Timer to = new Timer(true);

  System.out.println("Now=" + System.currentTimeMillis());
  to.schedule(new TimeOutTask("Short#1 - 250"), 250);
  to.schedule(new TimeOutTask("Long - 10050"), 10050);
  to.schedule(new TimeOutTask("Short#2 - 250"), 250);
  to.schedule(new TimeOutTask("Medium - 5050"), 5050);
  to.schedule(new TimeOutTask("Short#3 - 250"), 250);
}

All the TimeOutTask does is print the passed string and the current time. When the daemon flag is false the application does not terminate and I see this:

Now=1297705592543
Short#1 - 250:1297705592793
Short#3 - 250:1297705592793
Short#2 - 250:1297705592793
Medium - 5050:1297705597605
Long - 10050:1297705602605

When true the application terminates and I see this:

Now=1297705249422

I am just trying to find a way to monitor several tasks for time out purposes; I do not want the thread that monitors the time outs to keep the application from terminating. So I want daemon but when I make it a daemon none of my tasks seem to execute?!?!

EDIT:

Interesting, I think my problem stems from the way I try out new ideas in isolation. If I had a real app running it would keep the daemon thread alive and since these threads only represent time outs I don't care if they are done when the main app is finished.

I added this code to the end of my main method to pop up a frame and tested by closing it at different times. If I wait long enough all my threads execute, if I don't then the application terminates gracefully even though some tasks have not executed.

JFrame f = new JFrame("Test Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

Thanks for all your help.

like image 222
BigMac66 Avatar asked Jan 21 '23 12:01

BigMac66


2 Answers

When you make it a daemon thread, your main thread finishes immediately after scheduling the timers, and there's nothing stopping the application from quitting. You need to make up your mind: either you want the timer thread to keep the app alive, or you don't. You can't have it both ways :)

If you want to keep it alive just long enough for all the scheduled tasks to complete, then add an extra scheduled task which stops the timer, scheduling it for just after the latest task.

like image 73
Jon Skeet Avatar answered Feb 03 '23 21:02

Jon Skeet


Your JVM is terminating at the end of main(), as the other answers have described. You need to keep the application from exiting until the final tasks are done. Here is an (admittedly hacked together) example that pops up a window with a button to exit the application:

import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class TimerTest extends Frame implements ActionListener {
    Timer to;

    TimerTest() {
    to = new Timer(true);

        System.out.println("Now=" + System.currentTimeMillis());
        to.schedule(new TimeOutTask("Short#1 - 250"), 250);
        to.schedule(new TimeOutTask("Long - 10050"), 10050);
        to.schedule(new TimeOutTask("Short#2 - 250"), 250);
        to.schedule(new TimeOutTask("Medium - 5050"), 5050);
        to.schedule(new TimeOutTask("Short#3 - 250"), 250);

        Button b = new Button("Click to exit");
        b.addActionListener(this);
        this.add(b);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        TimerTest t = new TimerTest();
    }

    public void actionPerformed(ActionEvent a) {
        this.dispose();
    }
}

class TimeOutTask extends TimerTask {
    String s;

        TimeOutTask(String s) {
        this.s = s;
    }

    public void run() {
        System.out.println(s);
    }
}

The timed tasks will appear on the command line as you were trying to do in your example, but there is a frame that pops up. The "Click to exit" button will close the application regardless of whether the tasks are finished. I hope this example helps you out a bit.

like image 27
Michael K Avatar answered Feb 03 '23 22:02

Michael K