Solved

Maintaining timer for the program in java

Posted on 2012-04-02
13
414 Views
Last Modified: 2012-04-20
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...
0
Comment
Question by:gaugeta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
13 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 37799528
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() ;
0
 

Author Comment

by:gaugeta
ID: 37799547
@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...
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37799585
>>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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:mccarl
ID: 37799596
@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); }
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37799858
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
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37804313
> 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!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37804672
>>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?
0
 

Author Comment

by:gaugeta
ID: 37821107
@all:The program is not exitting after 30 mins.
0
 

Author Comment

by:gaugeta
ID: 37821114
@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.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 37822461
>>@all:The program is not exitting after 30 mins

I told you, try what I have suggested!
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37836029
> getMillis() method is not defined for Date.

Sorry, am used to an alternate Date/Time library. The method that you are after is getTime() is returns a long with the number of milliseconds since 1970.

> even after reassigning the start time inside the loop

You SHOULDN'T be re-assigning anything inside the loop. At the start of the program, and BEFORE entering the loop, you calculate the time at which it can stop, which as I said is done like below... (with method name fixed)

Date exitDateTime = new Date((new Date().getTime()) + 30 * 60 * 1000);

Open in new window


And then WITHIN the loop you are just checking if that exitTime has been reached, like below....

while(true) {

...

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

...

}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question