Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

looping every few minutes

Thread view: 
Mithil - 15 May 2007 21:26 GMT
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
Eric Sosman - 15 May 2007 21:53 GMT
> Hello everyone,
>
[quoted text clipped - 6 lines]
> 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.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Daniel Pitts - 15 May 2007 23:18 GMT
> > Hello everyone,
>
[quoted text clipped - 31 lines]
> Eric Sosman
> esos...@acm-dot-org.invalid

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!
Mithil - 18 May 2007 12:49 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.