Solved

Maintaining timer for the program in java

Posted on 2012-04-02
13
419 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 36

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 36

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 36

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

696 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