Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Java in General
Timer/ TimerTask
Jaan Smith
Greenhorn
Posts: 5
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How to schedule a timer only once. and when done with the task, the program should terminate. In my program the program does not terminates.
Thaks in Advance
Jaan
import java.util.Timer; import java.util.TimerTask; import java.util.Date; class Task extends TimerTask { private String name = null; public Task(String name){ this.name = name; } public void run() { System.out.println("*********every 7 sec ********** print my name-->"+name); } } public class Scheduler{ public static void main(String[] argc){ System.out.println("Wait for 7 second"); Scheduler s1 = new Scheduler(); s1.t(); } public void t(){ Timer timer = new Timer(); Task task = new Task("My Name"); timer.schedule(task, 5000); } }
Deepak Bala
Bartender
Posts: 6663
5
I like...
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Many ways. Do a Thread.sleep() or object.wait() instead of a timer. Or perform a system.exit(0) once you are done printing your name.
SCJP 6 articles
-
SCJP 5/6 mock exams
-
More SCJP Mocks
Jaan Smith
Greenhorn
Posts: 5
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The solution would be to cancel the timer in run method.
import java.util.Timer; import java.util.TimerTask; import java.util.Date; class Task extends TimerTask { private Timer timer = null; private String name = null; public Task(String name, Timer t){ this.name = name; this.timer = t; } public void run() { System.out.println("*********every 5 sec ********** print my name-->"+name); timer.cancel(); } } public class Scheduler{ public static void main(String[] argc){ System.out.println("Wait for 7 second"); Scheduler s1 = new Scheduler(); s1.t(); System.out.println("after the timer"); } public void t(){ Timer timer = new Timer(); Task task = new Task("My Name", timer); timer.schedule(task, 5000); //timer.cancel(); } }
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
To validate telnet connectivity in java code
problem with classes
Threads doubt
How to i invoke a method every 24 hours
Set Timer
More...