views:

99

answers:

3

I wrote a thread application which runs an infinite loop. The problem is since its defined as a thread the loop is not holding and process hostage but does use up a lot of my processing power hence reducing my battery life. Now I know this is not how normally programs a written but considering the fact that rouge applications may be built, what precaution does android take to stop processes which implement an infinite loop. While at it, is there a specific class I can access to access the memory usage by an application and also the processor usage?

The code...

package com.shouvik.HandlerThread;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class HandlerThread extends Activity{

private ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById(R.id.Button01)).setOnClickListener(new OnClickListener() 
    {
        public void onClick(View view) 
        {
            DoSomething();
        }       
    });
}

private Handler messageHandler = new Handler() {

    public void handleMessage(Message msg) 
    {
        super.handleMessage(msg);
        Log.v("Handler", "Message Received. Dismissing Dialog");
    }
};

protected void DoSomething() 
{
    new Thread() 
    {
        public void run() 
        {
                Log.v("Sleep", "Starting Count");
                for(int i= 1; i!=0; i++)
                {
                    Log.v("Count"," i = "+i);
                }
            messageHandler.sendEmptyMessage(0);
        }
    }.start();
}

}

Edited: Okay we know android does nothing to halt this program! So then how can we write a program to detect such flaws in programs and shut them down? Also I would like to know, is clicking on the button supposed to launch new threads of the same function while the previous ones are still running? Are they not supposed to be queued?

A: 

How about simply calling wait(100) at the end of the loop to to let it pause from time to time?

Christian
I like it but it serves no purpose.. As a counter it has maximum drain on resources...
Shouvik
A: 

it's going to draw batter life on every device or computer

you just have endless loop and are performing calculations not waiting for any events/actions

systems are normally just gonna let this program run endlessly

it's your program and you are responsible for how it works

if it runs a endless loop it's going to draw battery life.. simple as that

fazo
Yeah, but its fine as long as the programmer is morally responsible, but what if he is not! In that case, is there any way I can detect and handle such a problem via a program?
Shouvik
If the programmer is morally irresponsible, isn't an infinite loop the least of your worries?
Ken
I am sure android helps us deal with the other problems... The sandboxing technique works, programs which we download with certain permissions we would not like it to have we would not give it to it by refusing to install, but in case of a situation where the program seems harmless, we have serious issues if we are not able to detect such errors. Also as seen by certain researches people who will install fart applications will install almost anything. So in that case even if the user is technically qualified he is at an disadvantage (me included :P).
Shouvik
The user can view which applications consume how much of his battery and kick out those applications that are unreasonable.
Christian
+1  A: 

Read the Multitasking the Android way blog post for more information on how Android handles threads. Eventually if the memory gets low and the OS decided that your thread is not really important to the user android will kill the thread. If you implement this as a service though you can build it with an auto restart function that makes the OS recreate the service if there is memory available again.

Since the system can not decide if a program is doing good work. Maybe the app iscalculating really heavy numbers with the consent of the user for hours or just gone wild. There is no way for the system to decide this.

The only way a user can protect against this is to check the running processes and the battery consumption if the phone gets slow and maybe remove programs that are badly build and use to much resources, if some users are technically qualified enough I hope they rate and comment on the app in the market and warn other not that tech savvy people.

Janusz
I don't think I could have got a better answer than this one, though I was hoping android would seriously have some way to tell even ill-informed users about a harmful app! Thanks and +1 for your effort =)
Shouvik
Android *does* have ways to inform users about this; that's why 2.0 includes the "Battery Use" application, built right into the core of the system. Generally acccessible via Settings > About > Battery > Battery Usage.
Christopher