使用Thread.interrupt定期唤醒Android上的线程有什么缺点吗?线程循环看起来类似于以下内容:
public void run()
{
while(true)
{
try
{
wait();
}
catch(InterruptedException e)
{
performWork();
}
}
}发布于 2014-06-26 12:23:52
是。这是一种可怕的编码方式。例如,如果线程在I/O中被阻塞,并且不能像这样使用,interrupt()就会抛出异常。
相反,请使用通知/等待,它是为此创建的。在run()中是这样的
synchronized (this) {
while (conditionForWaiting) {
try {
wait();
} catch (InterruptedException ex) {}
}
performWork();并通知线程conditionForWaiting已更改:
synchronized (threadInstance) {
threadInstance.notify();
}https://stackoverflow.com/questions/24430308
复制相似问题