Solved

Maintaining timer for the program in java

Posted on 2012-04-02
13
390 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
java set up 1 46
groovy example issue 10 66
Java asynchronous logging 4 34
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now