PDA

View Full Version : How to loop a program?



mast3rpyr0
10-04-2008, 11:10 PM
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?

snoslicer8
10-04-2008, 11:20 PM
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.

mast3rpyr0
10-04-2008, 11:23 PM
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.

jasont
10-04-2008, 11:36 PM
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.

glassmw
10-05-2008, 01:43 AM
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?

glassmw
10-05-2008, 01:46 AM
maybe you can try some nested loops? not exactly sure what you're trying to do here, need more description.

dkvitus
10-05-2008, 01:58 AM
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.

dkvitus
10-05-2008, 02:08 AM
Just an FYI, I dont know if anyone used the book "Thinking in Java" by Bruce Eckel, but you can get it here (http://www.planetpdf.com/developer/article.asp?ContentID=6632)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);
}

Stanovoy
10-05-2008, 04:53 AM
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.

mast3rpyr0
10-05-2008, 04:42 PM
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

mast3rpyr0
10-05-2008, 05:06 PM
ok so i took out every sleep and wait i had in the program and it still does it. so its either whining cause theres an infinite loop or because i finish() the the class that starts the service and thus it cant be found. is there another way to do this?

Stanovoy
10-06-2008, 12:27 AM
It's not likely due to the loop itself then. PM me your code and I'll take a look at it.