java start 多次调用_在同一线程上两次调用start方法是否合法?

非常正确。 从文档中:

一次启动一个线程永远是不合法的。特别是,线程一旦完成执行就可能不会重新启动。

在重复计算方面,似乎可以使用SwingUtilities invokeLater方法。您已经在尝试run()直接调用,这意味着您已经在考虑使用a Runnable而不是raw了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工作中的invokeLater等效Handler.post(Runnable)。从其javadoc:

/**

* 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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值