开发者

Proper way to make java.util.Timer a daemon

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 i开发者_如何学Pythonf 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.


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.


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.


Javas definition of daemon Thread is that they don't stop your application from terminating. Once only daemon threads remain the jvm instance terminates, killing all remaining daemon threads.

They are not meant to run your application in the background, if you want to do this you have to start it as a background task or spawn a new jvm in a platform dependent way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜