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?
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.
How about simply calling wait(100)
at the end of the loop to to let it pause from time to time?
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
精彩评论