android 如何优雅的主动终止并等待线程的结束?


这段代码用destroy终止线程,也能用,貌似还是有问题的:

   
  private void viewOnline() {
  

if (currentNewsletter == null) {
Log.e(Constants.APP_NAME, "No newsletter selected");
return;
}

final ProgressDialog d = new ProgressDialog(this);
d.setMessage("Downloading...");
d.show();

final Context context = getApplicationContext();
t = new Thread(new Runnable() {
public void run() {

String fileName = currentNewsletter.mFilename;

Log.d(Constants.APP_NAME, "Downloading/showing: " + fileName);
final File file = Utilities.getFileFromURL(context, currentNewsletter.mUrl, currentNewsletter.mExpectedSizeInBytes, fileName, false);

d.dismiss();
// Now we can show the file
viewPDF(file);
}
});
t.start();

Context.MODE_WORLD_WRITEABLE).getAbsolutePath());

}

private void viewPDF(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");

try {
startActivity(intent);
} catch (Exception e) {
Toast.makeText(this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onStop() {
finish();
super.onStop();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
t.destroy();
Intent i = new Intent(NewslettersActivity.this,MainMenuActivity.class);

startActivity(i);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

虽然程序没崩溃,但是会有很多异常的log,说明这方法有点问题,有没有更温柔的方法让一个线程终止并等待其结束?

   
  11-09 11:42:28.740: E/AndroidRuntime(1538): FATAL EXCEPTION: main
  
11-09 11:42:28.740: E/AndroidRuntime(1538): java.lang.NoSuchMethodError: Thread.destroy()
11-09 11:42:28.740: E/AndroidRuntime(1538): at java.lang.Thread.destroy(Thread.java:600)
11-09 11:42:28.740: E/AndroidRuntime(1538): at com.rathbones.src.NewslettersActivity.onKeyDown(NewslettersActivity.java:144)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
11-09 11:42:28.740: E/AndroidRuntime(1538): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.os.Looper.loop(Looper.java:123)
11-09 11:42:28.740: E/AndroidRuntime(1538): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 11:42:28.740: E/AndroidRuntime(1538): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 11:42:28.740: E/AndroidRuntime(1538): at java.lang.reflect.Method.invoke(Method.java:521)
11-09 11:42:28.740: E/AndroidRuntime(1538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 11:42:28.740: E/AndroidRuntime(1538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 11:42:28.740: E/AndroidRuntime(1538): at dalvik.system.NativeStart.main(Native Method)
11-09 11:42:28.760: W/ActivityManager(59): Force finishing activity com.rathbones.src/.NewslettersActivity

Android 多线程

hcwjohn 12 years, 4 months ago

Java线程的常见的几种状态1.new(新建)。2.runnable(就绪)。3.Running(正在运行)。4.Block(阻塞)。5.sleep(睡眠)6.Dead(死亡)。
当run()方法或main()方法结束后,线程就进入终止状态,也就是结束啦。
你想办法让run()执行到最后就可以了, 发出中断信息是API中所推荐的 Thread.interrupt(),
简单的还可以设置一个BOOLEAN 值去控制, 一般你需要控制结束大多都是线程里面会是一个循环机制,每一次循环检测这个BOOLEAN 值变量即可。
按照上述,就是finish() 之后改变BOOLEAN 之后就行了。

fyyoj answered 12 years, 4 months ago

Your Answer