螺纹和电池消耗 [英] Thread and battery consumption

查看:91
本文介绍了螺纹和电池消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序,检查一些的手机每5秒的状态。 我做了如下:

I am working on app that check some of the status of phone every 5 seconds. I done it that:

Thread checking = new Thread() {
    public void run(){
        while( <<some status>> ) {
            <<checkstatus and do something>>
            Thread.sleep(5000);
        }
    }
}

这是坏的电池? 我怎样才能做到这一点,在其他的方式?服务站几秒钟后继续工作。

This is bad for the battery? How can I do it in the other way? Service stop working after couple seconds.

推荐答案

要回答你的问题,这取决于你正在做的,而循环里面有什么。

To answer your question, it depends what you are doing inside the while loop.

如果您不使用和醒来的任何看病贵的组件(如网络,蓝牙,其他传感器,和屏幕),你应该罚款。从本质上讲,这是操作系统的工作方式在后台反正,用一个线程是最便宜的方式之一。

If you are not using and waking up any "expensive" components (such as internet, bluetooth, other sensors, and screen), you should be fine. Essentially this is how the OS works in the background anyways and using a Thread is one of the cheapest ways.

另外,如果你的任务是CPU密集型的,你会耗尽电池为好。

Also, if your task is CPU intensive, you would drain the battery as well.

如果你这样做的权利你的服务应该可以正常工作。

Your service should work fine if you do it right.

另一种方法是使用处理程序,这将抽象多线程管理层对你的。

Another way would be using Handlers, which will abstract much of the thread managements for you.

这篇关于螺纹和电池消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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