This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to stop a Timer after it's run more than once? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to stop a Timer after it Watch "How to stop a Timer after it New topic

How to stop a Timer after it's run more than once?

Caly LeeAnn
Ranch Hand

Joined: Nov 22, 2005
Posts: 55

I create a Timer object timer and instantiate it in the init() method in myServlet class.

And there is a stopSampling() method and a startSampling() method to stop and start the timer respectively.

My question is if I stop timer and start timer in turns, then they both work well. But if I start the timer tiwce (call the startSampling() method twice), then call the stopSampling() method, the timer isn't stopped. How can I stop the timer?

public class myServlet extends HttpServlet {

private static Timer timer = null;

public final void init(final ServletConfig sConfig) throws ServletException {
timer = new Timer();

private void stopSampling() {

private void startSampling() {
timer = new Timer();

private boolean sampling() {
boolean result = true;

final int delay = 1000;
final int peroid = 5000;

try {
timer.scheduleAtFixedRate( new TimerTask() {
public void run() { samplePattern1();}
}, delay, peroid);

} catch (Exception e) {
result = false;
} finally {
return result;
} // try
} // sampling

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
You are creating a new TimerTask each time. Either re-use your timer task, or add code to start the timer only if it is not already running.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Caly LeeAnn
Ranch Hand

Joined: Nov 22, 2005
Posts: 55
When timer.cancel() is called, all the scheduled tasks associated to the timer will be discarded.
Any attempting to use the timer or the TimerTask object will cause an IllegalStateException to be thrown.

So I instantiate a TimerTask and the timer to avoid that. Any way to check the status of the timer and the TimerTask object?

The problem remains unsolved.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
First of all, creating a new Timer is completely unnecessary. You should reuse your Timer and create a new TimerTask instead. Likewise, you should cancel your TimerTask rather than cancelling your Timer. The second issue depends on your requirements:

1. Should it support concurrent sampling? In other words, if more than one call is made to startSampling() prior to a call to stopSampling() should more than one sampling task be started, or should duplicate calls to startSampling() be ignored if it is already sampling?

2. If concurrent sampling is necessary, should it behave in a FIFO or FILO manner? Meaning, should the first task started be the first task stopped, or should the most recent task started be the first task stopped?

If the answer to 1 is no, then you just need a simple flag that you set when sampling is started. Future calls will check this flag and only start sampling if you aren't already. Likewise, when sampling is stopped the flag is reverted and will only attempt to stop sampling if it hasn't stopped already. In this scenario, you can simply create a new TimerTask everytime you start sampling and stop on that TimerTask.

If the answer to 1 is yes, then you will need to keep a collection of tasks started. Each call to start sampling will create a new task and add it to the collection. Each call to stop sampling will pull the first task off the collection and stop it.

One task only:

Multiple with FIFO:

Multiple with FILO would be identical but with an ArrayList and remove() rather than removeFirst().
Caly LeeAnn
Ranch Hand

Joined: Nov 22, 2005
Posts: 55
Thanks alot Ken. It works perfectly fine now.
I agree. Here's the link:
subject: How to stop a Timer after it's run more than once?
Similar Threads
Updating GUI properties
Ending a thread from jsp
implementing a timed/scheduled task
servlet creating a new thread