完全正确。从文档中:
多次启动一个线程是违法的。特别是,线程在完成执行后可能不会重新启动。
就您可以为重复计算做些什么而言,似乎您可以使用SwingUtilities invokeLater方法。您已经在尝试直接呼叫,这意味着您已经在考虑使用 a 而不是原始 .尝试在任务上使用这种方法,看看它是否更适合你的思维模式。run()
Runnable
Thread
invokeLater
Runnable
下面是文档中的示例:
Runnable doHelloWorld = new Runnable() {
public void run() {
// Put your UI update computations in here.
// BTW - remember to restrict Swing calls to the AWT Event thread.
System.out.println("Hello World on " + Thread.currentThread());
}
};
SwingUtilities.invokeLater(doHelloWorld);
System.out.println("This might well be displayed before the other message.");
如果您将该调用替换为计算,它可能正是您所需要的。println
编辑:跟进评论,我没有注意到原始帖子中的Android标签。在 Android 工作中调用Later的等效方法是 。从它的javadoc:Handler.post(Runnable)
/**
* Causes the Runnable r to be added to the message queue.
* The runnable will be run on the thread to which this handler is
* attached.
*
* @param r The Runnable that will be executed.
*
* @return Returns true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting.
*/
因此,在Android世界中,您可以使用与上面相同的示例,将 替换为适当的帖子。Swingutilities.invokeLater
Handler