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 6679207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:19:59+00:00 2026-05-26T04:19:59+00:00

It would appear that the Java machine fails on certain types of loop that

  • 0

It would appear that the Java machine fails on certain types of loop that appear to be infinite to its system check but are in fact terminated by code outside the loop’s class. This does not happen often, but it was most noticeably found in my recent TickleService, where finite loops of code ran without trouble but loops that required ANY outside termination failed. For example:

class TickleService extends Service{
...
    void execute(){
        while(true){
            Log.e("Test","Tickle");
            wait(1000);
        }
    }
...
}

The above caused an immediate system ANR crash(CPU usage >99% exception).
However, the following version would run only once and cease further operation without ever crashing:

public class TickleService extends Service{
...
    public static boolean amNotAlive = false;
    void execute(){
        while(true){
          Log.e("Test","Tickle");
          if(amNotAlive)
            break;
          wait(1000);
        }
    }
...
}

The above loop executed once and ceased to loop.
Next, if the code had a finite loop it would execute flawlessly:

public class TickleService extends Service{
...
    public static boolean amNotAlive = false;
    void execute(){
        for(int i = 0; i<5;i++){
            Log.e("Test","Tickle");
            wait(1000);
        }
    }
...
}

This above loop would run 5 times each one second apart as expected.
Ultimately, the fix I applied was to run the following in which I tried to trick the JVM into thinking that no looping was occurring at all:

public class TickleService extends Service{
 ...
 public static boolean amAlive = true;
 void execute(){
    Log.e("Test","Tickle");
    wait(1000);
    if(amAlive)
      restartService(); //pseudo code for doing startService on self
 }
 ...
}

This trick executed without exception and reliably produced tickles one second apart.
Many tests were run, all confirmed that continuous loops failed if they terminated according to triggers outside the class, but ran correctly if terminated internally. Ultimately, while I strongly suspect either a purposeful design flaw or unintended bug in the Dalvik java machine causes this problem, but the evidence was only circumstantial. Furthermore, the problem does not seem to occur for all services that I have written in Android. Does anyone know why this is? I would love to understand this problem better.

  • 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-26T04:19:59+00:00Added an answer on May 26, 2026 at 4:19 am

    The Dalvik Virtual Machine is running correctly in all cases you have shown. Running infinite loops and thus blocking the main UI thread is poor practice and will only get you ANR errors. I haven’t seen your other code so I cannot respond to it.

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

Sidebar

Related Questions

It would appear that I am suffering from a new bug in IE7, as
I am a newbie working towards developing an IE extension that would appear as
I would like that the Index action doesn't appear in the url. For example,
I would like to do some stuff in Java that would be clearer if
I'm currently working on some concurrent code that would appear to have a few
Though this might appear as a duplicate of Java Web Services , I would
It would appear as though the only way to get the Java 6 version
I have been trying to parse Java exceptions that appear in a log for
I need a good spell checker library for Java that can spell check a
I have a java applet that I would like to include in my ASP.NET

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.