Given code is working in doInBackground(). Where while loop is always true but i don't know how it calls other methods in catch.
谁能向我解释这项技术以及我们如何从这项技术中获益。我不知道我们如何以及何时摆脱困境。
做背景
if(isRunning)
{
while (true) //this loop should run always.
{
try
{
Thread.sleep(1L);
}
catch (InterruptedException ex)
{
Log.e("Testing Interuption", "error=" + ex.getMessage());
// some working here is also running
}
}
}
while 之后是否可以调用任何语句?我的意思是它是否也可以退出 while 循环。
编辑
Interuption是什么时候发生的,意思是另一个AsyncTask在调用Thread.sleep()的时候;它会中断(意味着去 catch )。我说得对吗?
我正在调用 Multiple AsyncTasks
来设置 CameraPreview using Bitmap
。
@TargetApi(11)
public void start()
{
if (Build.VERSION.SDK_INT >= 11)
{
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
return;
}
execute(new Void[0]);
}
最佳答案
while (true)
语句永远不会结束,因为没有什么可以跳出循环的,所以不行,它无法跳出while循环。 (取决于 中的内容//此处的一些工作也在运行
。)
如果另一个线程向该线程发送中断,则执行catch
语句中的代码。当代码在 catch
下执行时,while
循环再次重新启动。
如果您想在收到 InterruptedException
时跳出 while 循环,请在 catch
中添加一个 break;
语句。
关于java - 如何有效地使用 InterruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822564/