简体   繁体   中英

Java Timer with thread

I develop a simple application and I use timer, but if I run the timer several times the timer drops this exception: Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Timer already cancelled. Here is my code:

public class Main {

...
private static void createAndShowUI() {
    ...
    //a listener of a radio button
    ActionListener on_action = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Timer.timer.schedule(Timer.task,0,2000);   //I call the timer here 
        }
    };
    ...
}
public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
 }


}
//and the class of timer:
public class Timer {


public static java.util.Timer timer = new java.util.Timer();
public static java.util.TimerTask task = new java.util.TimerTask() {


    public void run() {
        //some tasks
    }
};
}

My question is that where I use the thread? Thanks!

The problem is not using the Event-Queue thread, it is that you are re-using a cancelled Timer.

I'm guessing you are using the Timer to do some animation or something in response to a button push (as you schedule things at a fixed rate). I'm guessing also that in code you don't show us, the timer gets cancelled by a separate event. If you ever call Timer.cancel() can you show us that code?

From the exception what is happening is that you are trying to use the same Timer that you have already cancelled. Once a Timer has been cancelled, it can't be used again.

Two suggestions - use a different Timer every time. Also, if you are doing things for UI purposes, you might want to consider using a Swing timer instead .

As far as the Thread goes, all GUI events happen on the AWT Thread, but I repeat, this is almost certainly not the problem. Read this for more details.

A timer goes into cancelled state if either the cancel() method has been invoked or the if the timer task has terminated unexpectedly:

If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.

So in your case, it may not be a problem of where you put/call/use your time, but more a problem of what you're actually doing with your timer.

Here you have your thread:

Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially

so if you try to access your GUI from the Timer task you should put it into the EventQueue thread.

And look here

If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.

Do you let the Timer schedule any more tasks after it has been cancelled?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM