Java 定时器与线程

发布于 2024-11-03 02:48:37 字数 921 浏览 1 评论 0原文

我开发一个简单的应用程序并使用计时器,但如果我多次运行计时器,计时器会丢弃此异常:线程“AWT-EventQueue-0”java.lang.IllegalStateException 中的异常:计时器已取消。 这是我的代码:

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
    }
};
}

我的问题是我在哪里使用线程?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咆哮 2024-11-10 02:48:37

问题不在于使用事件队列线程,而在于您正在重新使用已取消的计时器。

我猜您正在使用计时器来执行一些动画或响应按钮按下的操作(当您以固定速率安排事情时)。我还猜测,在您没有向我们展示的代码中,计时器会被一个单独的事件取消。如果您曾经调用过 Timer.cancel() ,您可以向我们展示该代码吗?

从例外情况来看,发生的情况是您正在尝试使用已取消的同一个计时器。 一旦计时器被已取消,无法再次使用。

两个建议 - 每次使用不同的计时器。另外,如果您正在出于 UI 目的进行操作,您可能需要考虑 使用而是使用 Swing 计时器

就线程而言,所有 GUI 事件都发生在 AWT 线程上,但我再说一遍,这几乎肯定不是问题所在。 阅读本文了解更多详细信息。

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.

蝶舞 2024-11-10 02:48:37

如果调用了 cancel() 方法或者计时器任务意外终止,计时器就会进入已取消状态:

如果计时器的任务执行线程意外终止,例如,由于调用了其 stop 方法,则任何在计时器上调度任务的进一步尝试都将导致 IllegalStateException,就像调用了计时器的 cancel 方法一样。

因此,就您而言,这可能不是您在哪里放置/调用/使用时间的问题,而是您实际上在使用计时器做什么的问题。

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.

掐死时间 2024-11-10 02:48:37

这里有你的主题:

与每个 Timer 对象对应的是一个单独的后台线程,用于顺序执行所有计时器的任务

因此如果您尝试从 Timer 任务访问 GUI,则应将其放入 EventQueue 线程中。

看看这里

如果计时器的任务执行线程意外终止,例如,由于调用了其 stop 方法,则任何在计时器上调度任务的进一步尝试都将导致 IllegalStateException,就像调用了计时器的 cancel 方法一样。

取消后是否让计时器安排更多任务?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文