Link to home
Start Free TrialLog in
Avatar of GAUTAM
GAUTAMFlag for United States of America

asked on

Maintaining timer for the program in java

Hi Experts...
I have a requirement such that i need to run the java program only for a few minutes say 30 mins.
How can i maintain a timer such that the program terminates after 30 mins of running.
Please help...
Avatar of ksivananth
ksivananth
Flag of United States of America image

add the below code in your main method,

new Thread( new Runnable(){
  try{
    this.wait( 30 * 60 * 1000 ) ;
  }catch( InterruptedException ie ){}

  System.exit( 0 ) ;
}).start() ;
Avatar of GAUTAM

ASKER

@ksivananth:Thanks for the reply.
The thing is the program should have the control over terminating the program.
The program should be terminated only after checking some conditions along with the timer constraint.
Please help...
>>The program should be terminated only after checking some conditions along with the timer constraint
>>

define another flag/monitor class

public class ShutdownFlag{
  private boolean shutdown ;

  public synchronized boolean isShutdown(){
    return shutdown ;
  }

  public synchronized void setShutdown( boolean val ){
    shutdown = val ;
  }
}

ShutdownFlag shut = new ShutdownFlag() ;

new Thread( new Runnable(){
  while( ! shut.isShutDown() ){
  try{
    synchronized( shut ){
      shut.wait( 30 * 60 * 1000 ) ;
    }
  }catch( InterruptedException ie ){}
  }

  System.exit( 0 ) ;
}).start() ;


when the condition met, set true to shut!
Avatar of mccarl
@gaugeta...

Well then, you should read the system time when first starting your program and calculate the time that you want to exit, ie. Date exitDateTime = new Date((new Date().getMillis()) + 30 * 60 * 1000);    and then in your program where you are checking conditions, etc. you can exit the program when the current time is after the exit time, ie. if (new Date.after(exitDateTime)) { System.exit(0); }
how does it solve ur issue?

the accepted solution won't work at all, for e.g.,

if (new Date.after(exitDateTime)) { System.exit(0); }

if the condition return false, your program won't exit at all
> the accepted solution won't work at all

I tend to disagree, but lets continue...


> if the condition return false, your program won't exit at all

EXACTLY, the intended functionality! If the condition is false, it is because the program HASN'T been running for 30 minutes yet and it SHOULDN'T exit. But once the program has been running for 30 minutes, the above condition would return true, and hence the program will terminate.

Yep, I am pretty sure the solution will work!
>>But once the program has been running for 30 minutes, the above condition would return true, and hence the program will terminate.
>>

where does it check again?
Avatar of GAUTAM

ASKER

@all:The program is not exitting after 30 mins.
Avatar of GAUTAM

ASKER

@all:Here the error is it specifies the getMillis() method is not defined for Date.
And if i try for a method say for minutes or seconds the start time is not incrementing even after reassigning the start time inside the loop.
>>@all:The program is not exitting after 30 mins

I told you, try what I have suggested!
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial