Klausos Klausos
Klausos Klausos

Reputation: 16040

Run the method during prescribed number of minutes

There is a class Algorithm that has a method runAlgorithm. Currently it performs some predefined number of iterations, e.g. 100 iterations, after what it stops. This method is called from the class Test.

Now I need to update my code to be able to run the method runAlgorithm for a specified number of minutes, e.g. 5 minutes, after what it must be stopped.

As a result, I should be able to select the stopping criterion, i.e. time or number of iterations: algorithm.runAlgorithm('time',5) or algorithm.runAlgorithm('iterations',100).

I'm not sure how to do this. Should the class Algorithm be implemented as Runnable? Or do I need to create a timer in the class Test? A guidance will be highly appreciated.

public class Test {                                             

public static void main(String[] args) 
{

   init();

   Algorithm algorithm = new Algorithm();

   // 5 minutes is a stopping criterion for the algorithm
   Solution solution = algorithm.runAlgorithm('time',5);

   System.out.println(solution);

}

}

Upvotes: 0

Views: 100

Answers (5)

Tech Nerd
Tech Nerd

Reputation: 832

while(!Thread.currentThread().isInterrupted()){
for(i=1;i<100;i++){
    if(i==1){
    //call your method
    //use sleep (optional)
    }

    if(i==2){
    //call your method
    //use sleep (optional)
    }
    .
     .
      .
    if(i==5){
    //call your method
    //use sleep (optional)
    }
    if(i==100){

        Thread.currentThread().interrupt();

        break;
    }
    try {
        Thread.sleep(600);
    } catch (InterruptedException ex) {
        Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49362

Check The Guava library's TimeLimiter, which produces proxies that impose a time limit on method calls to the proxied object.

OR

Can you try this ?

private Solution runAlgorithm(String criterion, long minutes) {
   Solution solution = null;    
   if(criterion!=null && criterion.equalsIgnoreCase("time")){
        long startTime = System.currentTimeMillis();
        long endTime = startTime + minutes*60*1000;
        while (System.currentTimeMillis() < endTime)
        {
            // your code
        }
    }
    else {
            // run 100 iterations
     }
            return solution;
  }

Upvotes: 0

Synposis
Synposis

Reputation: 19

You can use a either a Java Timer, or other times with java (ie. Form Timers).

See Here For more info on that!

Upvotes: -1

Lucas
Lucas

Reputation: 14909

From the initial statement 100 iterations I assume runAlgorithm is basically a loop. Given that, you would just change the loop like so:

public Solution runAlgorithm( String method, int duration )
    Solution solution = null;
    if ( method.equals( "time" ) {
        long start = System.currentTimeMillis();
        while ( true ) {
            if ( System.currentTimeMillis() - start > duration ) {
                break;
            }
            // do stuff
        }
    }
    else {
        for ( int iter = 0; iter < 100; iter++ ) {
            // do stuff
        }
    }
    return solution;
}

Upvotes: 2

Tech Nerd
Tech Nerd

Reputation: 832

    while(!Thread.currentThread().isInterrupted()){
    for(i=1;i<100;i++){

        if(i==100){

            Thread.currentThread().interrupt();

            break;
        }
        try {
            Thread.sleep(600);
        } catch (InterruptedException ex) {
            Logger.getLogger(Thread2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

use a thread to run it for sometime (you want) after your time has been passed interrupt it its good because it gives more command and its concurrency as well you can edit the above code to change it according to your requirements thanks

Upvotes: 0

Related Questions