給定的代碼在doInBackground()中工作。 while循環總是如此,但我不知道它如何調用catch中的其他方法。如何有效地使用InterruptedException
有人可以解釋我的技術,我們如何受益於這種技術。我不知道如何以及何時離開循環。
doInBackground
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循環或不循環。
編輯
什麼時候當另一個的AsyncTask正在呼叫Thread.sleep()方法的Interuption Occur.It裝置;它會中斷(意味着趕上)。我對嗎?我要求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]);
}
查看我的編輯請。這意味着AsyncTask的另一個實例調用Thread.sleep();第一個實例調用中斷。我是否正確 – Nepster
不。Thread.sleep()唯一能做的就是讓線程進入休眠狀態。如果另一個線程進入睡眠狀態,而第一個線程不會以任何方式影響第一個線程。對於第一個被中斷的線程,一些線程需要調用[Thread#interrupt()](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt%28% 29)在線程上。 –
那麼爲什麼會發生interuptionException。我想知道它何時會發生,我怎麼能控制它。 – Nepster