Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TimerTask and Schedule

 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a small program which uses a timer and timer task package.. the program works fine.. but the problem is that as the time elapses i want to show the hour minutes and seconds .. is there a way to print it..
The program is as follows...
===========================================================================
package com.mylib.olrt.Timer;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;

public class TimerBean {

Timer timer;
Toolkit toolkit;

public TimerBean(int seconds)
{
timer=new Timer();
toolkit = Toolkit.getDefaultToolkit();
timer.schedule(new schedtask(),seconds*1000);
}

class schedtask extends TimerTask
{
public void run()
{
toolkit.beep();
System.out.println("Time's up!");
timer.cancel();

}
}

}
===========================================================================
package com.mylib.olrt.Timer;

public class Timertest
{
public static void main(String args[])
{
TimerBean tb=new TimerBean(5);
System.out.println("TAsk Scheduled!");
}

}

=============================================================================


Thank you
Regards
[ April 05, 2006: Message edited by: Ranganath .S.Junpal ]
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you will have to code it. Currently, you are using Timer to schedual a single event at a particular time. You will now have to schedule a repeating event, every second or so. This new event will now simply print the interium results, if the final time haven't been reached. And do what it did before, if the time has been reached (just don't forget to cancel the event).

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic