Solved

Maintaining timer for the program in java

Posted on 2012-04-02
13
410 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
  • 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 95
even odd program using while loop 3 72
Setup GlassFish 4 34
Website checklist for browser compatibility? 2 37
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

680 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