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.
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
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
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
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.
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?
maybe you can try some nested loops? not exactly sure what you're trying to do here, need more description.
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.
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);
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.
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
Bookmarks