File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes TimerTask and Schedule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TimerTask and Schedule" Watch "TimerTask and Schedule" New topic

TimerTask and Schedule

Ranganath .S.Junpal
Ranch Hand

Joined: Jan 16, 2005
Posts: 52
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()
System.out.println("Time's up!");


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
[ April 05, 2006: Message edited by: Ranganath .S.Junpal ]
Henry Wong

Joined: Sep 28, 2004
Posts: 20521

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).


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: TimerTask and Schedule
It's not a secret anymore!