Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序停止后线程仍在运行_Android_Multithreading - Fatal编程技术网

Android 应用程序停止后线程仍在运行

Android 应用程序停止后线程仍在运行,android,multithreading,Android,Multithreading,我的主要活动是做一些初始的事情,然后启动InitDBActivity。这一个应该检查我的数据库是最新的,并在必要时更新它。以下是在InitDBActivity的onCreate方法中完成的 Thread asyncDBCreation = new Thread(new Runnable() { public void run() { SQLiteDatabase db = null;

我的主要活动是做一些初始的事情,然后启动InitDBActivity。这一个应该检查我的数据库是最新的,并在必要时更新它。以下是在InitDBActivity的onCreate方法中完成的

Thread asyncDBCreation = new Thread(new Runnable() {
                public void run() {

                    SQLiteDatabase db = null;
                    try{
                        db = dbh.getWritableDatabase();
                    }catch(Exception ex){
                        return;
                    }

                    if(dbh.recreateDB(db)){
                        try{
                            dbh.insertQueries(dbh.getQueries(getAssets()),db);
                        }catch(Exception ex){
                            dbh.close();
                            finish();
                            return;
                        }
                        dbh.close();

                        prefEdit.putLong(dbVersion, dbFileVersion);
                        prefEdit.commit();

                        startActivity(intent);
                        finish();
                    }
                    else{
                    }
                }
            });
            asyncDBCreation.start();

问题是,如果用户单击“后退”按钮,他将退出应用程序,但该线程仍在后台运行。一切都会好起来的——不管怎么说,这份工作都没有了,但是。。。结果是,如果他在这个线程完成之前再次启动应用程序,一切都会一团糟。数据库中的数据成倍增加。如何终止此线程?

在java中,没有方法可以终止线程,只有中断线程,您可以使用方法:
thread.interrupt()
中断线程,但线程必须处于可中断的状态,如:sleep或wait


因此,在这里您可以使用
db.close
方法关闭数据库,数据查询工作将抛出异常,您可以捕获异常以完成线程工作。

管理在Activity.onCreate()中创建的线程,并使用来终止Activity.onDestroy()中正在运行的线程:


Cancel方法将根据您的任务运行状态进行不同的行为,请查看API以了解更多详细信息

ThreadPoolExecutor ThreadPoolExecutor=Executors.newSingleThreadExecutor();给出错误:类型不匹配:无法从ExecutorService转换为ThreadPoolExecutor@gisek,使用ExecutorService或将其强制转换为ThreadPoolExecutor我使用了ExecutorService。调试器进入longRunningTaskFuture.cancel(true),但它不会停止此运行。@gisek,检查cancel()返回的布尔值,如果无法取消任务,则为false,通常是因为任务已正常完成;反之亦然
private Future longRunningTaskFuture;

public void onCreate(Bundle savedInstanceState) {
  ... ...
  ThreadPoolExecutor threadPoolExecutor = Executors.newSingleThreadExecutor();
  Runnable longRunningTask = new Runnable();
  // submit task to threadpool:
  Future longRunningTaskFuture = threadPoolExecutor.submit(longRunningTask);
}

... ...
public void onDestroy() {
  // if you want to kill the task:
  if (longRunningTaskFuture != null)
    longRunningTaskFuture.cancel(true);
  ... ...
}