Android Community
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: How to loop a program?

  1. #1
    Join Date
    Sep 2008
    Posts
    683

    Default How to loop a program?


    So i got a service running in the background on my app. right now i have it looping to update something on my webserver every 10 seconds like so:

    While(1==1)
    {
    ...
    Thread.sleep(10000);
    }

    i assume there is a far better alternative for doing this?

    Sprint HTC Evo 4G - Rooted
    CyanogenMod 6.0.0 Froyo 2.2 ROM
    CM6-Snap 7.6

    Check out my site Exodus Apps, we will be developing applications for the Notion Ink Adam Tablet soon!

    Follow us on Twitter @Exodusapps
    Follow me on Twitter @Aaron_Blank

  2. #2
    Join Date
    Sep 2008
    Location
    St. Louis, MO
    Posts
    554

    Default Re: How to loop a program?

    Speaking in basic Java terms, that's how I was taught to loop...I don't know of any way that is more or less efficient.
    Registered Nurse by day,
    Tech Head by...well...always.
    T-Mobile MyTouch 3G [Merlot]
    ROM: CyanogenMod 4.1.11.1 Recovery: CM 1.4

  3. #3
    Join Date
    Sep 2008
    Posts
    683

    Default Re: How to loop a program?

    well the thing with this right now (i think thats whats causing it) after about 10-15 seconds of the service running, it says the program is not respoding, and if i hit wait it will keep going just fine but thats a hassel. maybe 10 sec is to long to pause? but i really need it to update maybe every minute.

    Sprint HTC Evo 4G - Rooted
    CyanogenMod 6.0.0 Froyo 2.2 ROM
    CM6-Snap 7.6

    Check out my site Exodus Apps, we will be developing applications for the Notion Ink Adam Tablet soon!

    Follow us on Twitter @Exodusapps
    Follow me on Twitter @Aaron_Blank

  4. #4
    Join Date
    Sep 2008
    Location
    California
    Posts
    88

    Default Re: How to loop a program?

    Instead of 1==1 I would use a boolean while(running) which you can turn off/on when you need.

    Instead of sleeping for 10s every cycle, I would track when the method started and when it finished. From these 2 values decide if it needs to sleep X or not.

  5. #5
    Join Date
    Sep 2008
    Posts
    24

    Default Re: How to loop a program?

    would it not be responding because that is an infinite loop?
    1==1 is always true therefore it never leaves. or is that the point?

  6. #6
    Join Date
    Sep 2008
    Posts
    24

    Default Re: How to loop a program?

    maybe you can try some nested loops? not exactly sure what you're trying to do here, need more description.

  7. #7
    Join Date
    Oct 2008
    Location
    Honolulu, HI
    Posts
    689

    Default Re: How to loop a program?

    Wouldnt it be easier to just use:

    while (true){

    //Enter Code Here

    } //end while statement

    If it says the program is not responding then check the code in the 'while' block, it may be a simple syntax issue you overlooked.

  8. #8
    Join Date
    Oct 2008
    Location
    Honolulu, HI
    Posts
    689

    Default Re: How to loop a program?

    Just an FYI, I dont know if anyone used the book "Thinking in Java" by Bruce Eckel, but you can get it here in pdf - FREE. They even have 1st 2nd and 3rd editions

    I double checked and you can find an example of an infinite loop on Page 163 (Due to the Table of Contents, Go to Page 194/1182)

    Excerpt:
    // An "infinite loop":
    while(true) {
    i++;
    int j = i * 27;
    if(j == 1269) break; // Out of loop
    if(i % 10 != 0) continue; // Top of loop
    System.out.println(i);
    }

  9. #9
    Join Date
    Sep 2008
    Location
    Washington, DC
    Posts
    664

    Default Re: How to loop a program?

    The problem is with your Thread.sleep(). When you call that, the thread is not running anymore. It is only reactivated when the processor gets the signal that the time period specified has lapsed, i.e. the thread itself does not monitor the timeout itself. During this time, if you or anything else want to operate on the thread, the thread will not respond. Here's what you may want to do:

    int pause = 10000
    int interval = 50;
    int counter;

    while (true)
    {
    // your logic here

    counter = 0;

    do
    {
    Thread.sleep(interval);
    counter += interval;
    } while (counter < pause)
    }

    This way, you allow the thread to be interrupted without overburdening the processor time slices. Furthermore, by using simple integer additions, the gap between sleeps is not processor intensive; thus, it won't be a noticeable performance hit when you do it 20 times a second.
    Last edited by Stanovoy; 10-05-2008 at 04:01 AM.

  10. #10
    Join Date
    Sep 2008
    Posts
    683

    Default Re: How to loop a program?


    Quote Originally Posted by Stanovoy View Post
    The problem is with your Thread.sleep(). When you call that, the thread is not running anymore. It is only reactivated when the processor gets the signal that the time period specified has lapsed, i.e. the thread itself does not monitor the timeout itself. During this time, if you or anything else want to operate on the thread, the thread will not respond. Here's what you may want to do:

    int pause = 10000
    int interval = 50;
    int counter;

    while (true)
    {
    // your logic here

    counter = 0;

    do
    {
    Thread.sleep(interval);
    counter += interval;
    } while (counter < pause)
    }

    This way, you allow the thread to be interrupted without overburdening the processor time slices. Furthermore, by using simple integer additions, the gap between sleeps is not processor intensive; thus, it won't be a noticeable performance hit when you do it 20 times a second.

    hmm that still makes the error

    Sprint HTC Evo 4G - Rooted
    CyanogenMod 6.0.0 Froyo 2.2 ROM
    CM6-Snap 7.6

    Check out my site Exodus Apps, we will be developing applications for the Notion Ink Adam Tablet soon!

    Follow us on Twitter @Exodusapps
    Follow me on Twitter @Aaron_Blank

Page 1 of 2 1 2 LastLast

Similar Threads

  1. T-Mobile USA Details Dev Program details
    By refused9150 in forum General Phone Chat
    Replies: 0
    Last Post: 09-11-2008, 11:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •