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 / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Allow a Java iterative statement to run for only a certain period

Thread view: 
Peter - 19 Apr 2004 18:33 GMT
Hi,

How could I only allow a Java iterative statement to run for only a certain
period. Such as running a while loop for 10 seconds...

Thanks

Signature

Peter Wong
peter@peterwong.org
http://www.peterwong.org

Chris Smith - 19 Apr 2004 19:21 GMT
> How could I only allow a Java iterative statement to run for only a certain
> period. Such as running a while loop for 10 seconds...

   long startTime = System.currentTimeMillis();
   long period = 10000; // 10 seconds = 10000 millis

   while (otherCondition
       && (System.currentTimeMillis() < startTime + period))
   {
       ...
   }

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Matt Humphrey - 19 Apr 2004 19:24 GMT
> Hi,
>
> How could I only allow a Java iterative statement to run for only a certain
> period. Such as running a while loop for 10 seconds...

long finishTime= System.currentTimeMillis () + 10 * 1000;

while ( (your condition) && (System.currentTimeMillis () < finishTime) ) {
 // Do your stuff
}

if (your conditon) {
 it finished ok
} else {
 it timed out
}

This cannot be more accurate than the underlying time base, the fairness of
the scheduler and the amount of time used by each loop iteration.  If each
iteration is long you may want to consider using a Thread and having the
iteration poll a stop flag at a wide variety of points.  Or checkout
thread.join (time).

Cheers,
Matt Humphrey  matth@ivizNOSPAM.com  http://www.iviz.com/


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.