Android的线程管理的onPause [英] android thread management onPause

查看:121
本文介绍了Android的线程管理的onPause的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承Thread类,并具有如此实现它的run方法。

I have a class that extends the Thread class and has its run method implemented as so.

public void run(){
    while(!terminate){
        if(paused){
            Thread.yield();
        }else{
            accummulator++;
        }
    }
}

此线程从onCreate方法产生了。

This thread is spawned from the onCreate method.

在我的用户界面是隐藏(当Home键为pressed)我的onPause方法将设置暂停标志为真,产生胎面。然而,在DDMS我仍然看到线程积累的UTIME和状态运行。

When my UI is hidden (when the Home key is pressed) my onPause method will set the paused flag to true and yield the tread. However in the DDMS I still see the uTime of the thread accumulate and its state as "running".

所以我的问题是。什么是正确的方式来停止线程,以便它不使用占用CPU时间?

So my question is. What is the proper way to stop the thread so that it does not use up CPU time?

推荐答案

这其实是不好的做法,以保持一个线程的onPause运行后。其原因是,在onPause后您的应用程序可以在任何时候砸出来的内存没有你能够知道,所以你将不能够照顾自己清理。

It is actually bad practice to keep a thread running after onPause. The reason is that after onPause your application may drop out of memory at any time without your being able to know, therefore you will not be able to clean up after yourself.

正确的方法做这件事是停止线程的onPause并重新创建它onResume。如果你需要说明你可以使用Android内置的saveState和方法,或者设置或任何以保持这一点。

The proper way to do it is stopping the thread onPause and recreating it onResume. If you need state you can use Android's built in saveState methods or settings or whichever to keep that.

这篇关于Android的线程管理的onPause的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆