我正在像这样开始我的线程:
public void startTask(Runnable r)
{
running = true;
Log.i(tag, "-----------start.Runnable-----------");
final Thread first = new Thread(r);
currentTask = first;
first.start();
Thread second = new Thread(new Runnable() {
@Override
public void run() {
try{
first.join();
}
catch(InterruptedException e){
}
running = false;
}
});
second.start();
}
当我想取消当前操作时,我正在根据这篇文章使用 Theread:http ://forward.com.au/javaProgramming/HowToStopAThread.html :
public void cancelTask()
{
Log.i(tag, "-----------cancelTask()-----------");
try{
currentTask.interrupt();
running = false;
}
catch(SecurityException e){
e.printStackTrace();
}
}
cancelTask 后应用程序仍在运行,但我有异常:
12-05 20:29:00.274: W/System.err(13533): java.lang.InterruptedException
12-05 20:29:00.274: W/System.err(13533): at java.lang.VMThread.sleep(Native Method)
12-05 20:29:00.294: W/System.err(13533): at java.lang.Thread.sleep(Thread.java:1047)
12-05 20:29:00.294: W/System.err(13533): at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:331)
12-05 20:29:00.304: W/System.err(13533): at com.rsd.myfirstapp3.ProgressServiceActivity$2.run(ProgressServiceActivity.java:174)
12-05 20:29:00.314: W/System.err(13533): at java.lang.Thread.run(Thread.java:864)
.
在使用线程的此类操作中是正常的异常吗?