Solved

Maintaining timer for the program in java

Posted on 2012-04-02
13
399 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

777 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