Can Android kill my app while it is in the middle of a loop execution?

随声附和 提交于 2019-12-23 11:44:22

问题


When Android decides to remove an application from the stack in order to free up some RAM, what happens if the application that is being destroyed is currently running some loop in the background? Will the loop be terminated amid execution or will the VM wait for it to finish?


回答1:


Will the loop be terminated amid execution or will the VM wait for it to finish?

The loop is terminated, otherwise it isn't quite "killing".

Simple test:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    Log.i("LOOP", "Running");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {}
        }
    }).start();
}
}

Swipe the app out from the recent apps.



来源:https://stackoverflow.com/questions/34463069/can-android-kill-my-app-while-it-is-in-the-middle-of-a-loop-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!