looping every few minutes

M

Mithil

Hello everyone,

I have written a code that goes through an infinite loop in the main
function and calls an other function and goes to sleep. In this
function I am awaiting for an input from the user, my program doesn't
wait for the users input when the sleep timer expires calling that
function again. Having a recursive function would blow up the stack as
it is an indefinite loop. Is there any other means of waiting for
users input and still make it an infinite loop which sleeps every few
minutes. Plz help me out I am really stuck at this situation.

Mithil
 
E

Eric Sosman

Mithil said:
Hello everyone,

I have written a code that goes through an infinite loop in the main
function and calls an other function and goes to sleep. In this
function I am awaiting for an input from the user, my program doesn't
wait for the users input when the sleep timer expires calling that
function again. Having a recursive function would blow up the stack as
it is an indefinite loop. Is there any other means of waiting for
users input and still make it an infinite loop which sleeps every few
minutes. Plz help me out I am really stuck at this situation.

There are a couple of ways to do this, but one of the
more flexible is to use two threads: One to do the repetitive
work, and one to wait for user input. The repetitive thread
does its parcel of work and then waits for notification that
input has arrived, using the wait(long) method to specify a
timeout. Upon awakening, it can check whether it's received
an input notice or has timed out. (It may also awaken for no
good reason, so if there's no input you should probably check
System.currentTimeMillis() to see if enough time has passed.)

Meanwhile, the input thread simply reads user input, taking
as long as it takes. When something arrives, it sets the "input
is here" indicator and calls notify() or notifyAll() to awaken
the repeating thread.

There are, as I say, other ways to do this. Some of the
other ways may be better suited to your situation -- but you
haven't told us much about your situation, so I've described an
approach that should cover pretty much everything, although it
may be more involved than you need.
 
D

Daniel Pitts

There are a couple of ways to do this, but one of the
more flexible is to use two threads: One to do the repetitive
work, and one to wait for user input. The repetitive thread
does its parcel of work and then waits for notification that
input has arrived, using the wait(long) method to specify a
timeout. Upon awakening, it can check whether it's received
an input notice or has timed out. (It may also awaken for no
good reason, so if there's no input you should probably check
System.currentTimeMillis() to see if enough time has passed.)

Meanwhile, the input thread simply reads user input, taking
as long as it takes. When something arrives, it sets the "input
is here" indicator and calls notify() or notifyAll() to awaken
the repeating thread.

There are, as I say, other ways to do this. Some of the
other ways may be better suited to your situation -- but you
haven't told us much about your situation, so I've described an
approach that should cover pretty much everything, although it
may be more involved than you need.

Actually, I'd go further than suggesting two threads, and suggest
using the java.util.Timer class. The Timer class starts up its own
thread, and manages that for you. One less thing to worry about.

Instead of awakening the timer thread on a user request, why not
simply do the thing that the user requests!
 
M

Mithil

HI,

Thanks guys I needed to get a starting point of somewhere. I think I
should be able to manage from now on cheers again guys and thanks for
replying this quick.

Mithil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top