Java 定时器与线程
我开发一个简单的应用程序并使用计时器,但如果我多次运行计时器,计时器会丢弃此异常:线程“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题不在于使用事件队列线程,而在于您正在重新使用已取消的计时器。
我猜您正在使用计时器来执行一些动画或响应按钮按下的操作(当您以固定速率安排事情时)。我还猜测,在您没有向我们展示的代码中,计时器会被一个单独的事件取消。如果您曾经调用过 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.
如果调用了
cancel()
方法或者计时器任务意外终止,计时器就会进入已取消状态:因此,就您而言,这可能不是您在哪里放置/调用/使用时间的问题,而是您实际上在使用计时器做什么的问题。
A timer goes into cancelled state if either the
cancel()
method has been invoked or the if the timer task has terminated unexpectedly: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.
这里有你的主题:
因此如果您尝试从 Timer 任务访问 GUI,则应将其放入 EventQueue 线程中。
看看这里
取消后是否让计时器安排更多任务?
Here you have your thread:
so if you try to access your GUI from the Timer task you should put it into the EventQueue thread.
And look here
Do you let the Timer schedule any more tasks after it has been cancelled?