Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:05:09+00:00 2026-05-15T23:05:09+00:00

I wrote a thread application which runs an infinite loop. The problem is since

  • 0

I wrote a thread application which runs an infinite loop. The problem is since its defined as a thread the loop is not holding the 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:05:10+00:00Added an answer on May 15, 2026 at 11:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use GestureDetector (Which is a better way to… May 16, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer So the problem is, doSomething could be implemented as: public… May 16, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer This is a fairly common technique in other languages. Almost… May 16, 2026 at 4:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.