Timer in Java

Discussion in 'Java' started by joshivaibhav, Dec 19, 2006.

  1. joshivaibhav

    joshivaibhav Guest

    Hello,

    I am running the following code, but it never terminates:


    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;

    public class YetAnotherTimer
    {
    private static Integer iCount = 0;
    public static void main (String args[])
    {
    int numberOfMillisecondsInTheFuture = 10000; // 10 sec

    Date timeToRun = new
    Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);

    Timer timer = new Timer();

    timer.schedule(new TimerTask()
    {
    public void run()
    {
    System.out.println("Generating report");
    iCount++;
    System.out.println("Count is: " + iCount);
    }
    }, timeToRun);
    }
    }

    It simply waits in the memory. I am looking for functionality where the
    timer will execute a task at a predefined time and once the task is
    finished, the timer thread will exit. I tried calling the method
    cancel() from within the run() method, but still it does not terminate
    the timer thread.

    Please let me know.

    Thanks,

    Vaibhav
    joshivaibhav, Dec 19, 2006
    #1
    1. Advertising

  2. joshivaibhav

    Daniel Pitts Guest

    joshivaibhav wrote:
    > Hello,
    >
    > I am running the following code, but it never terminates:
    >
    >
    > import java.util.Date;
    > import java.util.Timer;
    > import java.util.TimerTask;
    >
    > public class YetAnotherTimer
    > {
    > private static Integer iCount = 0;
    > public static void main (String args[])
    > {
    > int numberOfMillisecondsInTheFuture = 10000; // 10 sec
    >
    > Date timeToRun = new
    > Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);
    >
    > Timer timer = new Timer();
    >
    > timer.schedule(new TimerTask()
    > {
    > public void run()
    > {
    > System.out.println("Generating report");
    > iCount++;
    > System.out.println("Count is: " + iCount);
    > }
    > }, timeToRun);
    > }
    > }
    >
    > It simply waits in the memory. I am looking for functionality where the
    > timer will execute a task at a predefined time and once the task is
    > finished, the timer thread will exit. I tried calling the method
    > cancel() from within the run() method, but still it does not terminate
    > the timer thread.
    >
    > Please let me know.
    >
    > Thanks,
    >
    > Vaibhav


    Look into the Timer(boolean isDaemon) constructor.
    Daniel Pitts, Dec 19, 2006
    #2
    1. Advertising

  3. joshivaibhav

    joshivaibhav Guest

    I found the problem:

    If I use the timer.cancel(); after the statement:

    System.out.println("Count is: " + iCount);

    The timer terminates properly.

    joshivaibhav wrote:
    > Hello,
    >
    > I am running the following code, but it never terminates:
    >
    >
    > import java.util.Date;
    > import java.util.Timer;
    > import java.util.TimerTask;
    >
    > public class YetAnotherTimer
    > {
    > private static Integer iCount = 0;
    > public static void main (String args[])
    > {
    > int numberOfMillisecondsInTheFuture = 10000; // 10 sec
    >
    > Date timeToRun = new
    > Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);
    >
    > Timer timer = new Timer();
    >
    > timer.schedule(new TimerTask()
    > {
    > public void run()
    > {
    > System.out.println("Generating report");
    > iCount++;
    > System.out.println("Count is: " + iCount);
    > }
    > }, timeToRun);
    > }
    > }
    >
    > It simply waits in the memory. I am looking for functionality where the
    > timer will execute a task at a predefined time and once the task is
    > finished, the timer thread will exit. I tried calling the method
    > cancel() from within the run() method, but still it does not terminate
    > the timer thread.
    >
    > Please let me know.
    >
    > Thanks,
    >
    > Vaibhav
    joshivaibhav, Dec 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    700
    Kelsang Wangchuk
    Jul 31, 2003
  2. Replies:
    1
    Views:
    1,564
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  3. Simon

    page timer, or redirect timer

    Simon, Nov 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,167
    Bruce Barker
    Nov 4, 2005
  4. amy j
    Replies:
    4
    Views:
    867
    Alex Hunsley
    Oct 13, 2004
  5. Replies:
    8
    Views:
    467
    Jorgen Grahn
    Jul 15, 2013
Loading...

Share This Page