Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3958662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:38:09+00:00 2026-05-20T02:38:09+00:00

I have the following java.util.Timer but it does not seem to execute and I

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:38:10+00:00Added an answer on May 20, 2026 at 2:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: package application; import java.util.Timer; import java.util.TimerTask; public class Application
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Say suppose I have the following Java code. public class Example { public static
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
I have the following implicit conversion for java.util.Enumerations implicit def enumerationIterator[A](e : Enumeration[A]) :
Say I have the following config for the java util logger I am using
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
I have the following code which does nothing but reading some values from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.