Problems with Java timer class.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
I'm trying to use the timer class to repeat an action after a fixed amount of time, but I can't even get it to print the message once, as I keep getting the error. "exception in thread "Timer-0" java.lang.IllegalStateException: Task already scheduled or cancelled
 
Last edited:
You're running the task in your constructor, and you're running your task in the squeeze method. You seem to have completely dis-regarded the timer altogether even though you created it, so you are trying to run the task outside of the timer (i.e. you're still single threaded).

Furthermore, your stack trace seems to indicate you ARE using the timer, have you modified the code since or before you posted the trace / snippet?

P.S I don't see why you would use the Timer class, it's more a class to schedule something to happen at some given time, if you want something to go NOW and increment on a certain delay, you might as well just use a Thread. (Don't forget to stop the thread when they release the pump handle though, you might wanna keep the thread as an instance variable so you can call the interrupt method. Also, note, you cant restart a thread, but that's alright, just create a new one passing in the same Runnable as an anonymous class. (i.e. stop and nullify the instance variable, then create the new thread in the instance variable)

If you wanted to get fancy there you could use an executor as a way of re-using a thread, or have a constantly running side thread that your app can just observe (observer design pattern) when it needs to know when a tick happens. But tbh, those two things are huge amounts of overkill.
 
Last edited:
In your class if you have the method you want to run over and over, adding something like

Code:
private javax.swing.timer T; //Add this line to your variable declarations 


//put this in your constructor
Runnable r = new Runnable(){
	public void run(){
		T = new javax.swing.Timer(1000, new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				YOURMETHOD();
			}
		});
	}

};

if(javax.swing.SwingUtilities.isEventDispatchThread()){
	r.run();
}else{
	javax.swing.SwingUtilities.invokeLater(r);
}



//put this in your squeeze nozzle method
T.start(); 

//and when you want to stop dispensing
T.stop();

Instead of using absolute class names you can add them as imports at the top of your class file (which is nicer, but would have been more difficult to provide an example.

Replace the YOURMETHOD(); line with what you want to happen every time the timer clicks.

Hope that helps.
 
Last edited:
Are you starting and stopping the timer correctly? While the timer is running the above will call YOURMETHOD() every second.

Can you show your current source?
 
Where exactly do you squeeze the nozzle, if that's the whole application, the squeezenozzle() method is never called, and as such the timer is never started.

It probably increments once as you have a call to dispensepetrol() at the end of your constructor.
 
It probably increments once as you have a call to dispensepetrol() at the end of your constructor.


Ah right, thats how I was testing whether the method works or not. I'll try it by calling the method via another method.
 
Last edited:
Back
Top Bottom