Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Timer in Java

Thread view: 
joshivaibhav - 19 Dec 2006 20:39 GMT
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
Daniel Pitts - 19 Dec 2006 21:27 GMT
> Hello,
>
[quoted text clipped - 39 lines]
>
> Vaibhav

Look into the Timer(boolean isDaemon) constructor.
joshivaibhav - 19 Dec 2006 22:06 GMT
I found the problem:

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

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

The timer terminates properly.

> Hello,
>
[quoted text clipped - 39 lines]
>
> Vaibhav


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.