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?