0

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);

}

}
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • @syb0rg: I want the method runAlgorithm to run specific number of milliseconds. So, time as a stopping criterion for the algorithm. For instance, if I specify 5 minutes as a stopping criterion, the algorithm must be runned 5 minutes. After 5 minutes (it may refer to e.g. 133 iterations) it must provide solutions. – Klausos Klausos Apr 28 '13 at 17:01

5 Answers5

2

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;
}
Lucas
  • 14,227
  • 9
  • 74
  • 124
0

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;
  }
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0
    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

Tech Nerd
  • 822
  • 1
  • 13
  • 39
  • you can set the iterations you want and the time here I have set it 1min – Tech Nerd Apr 28 '13 at 17:11
  • In this case where do I need to paste algorithm.runAlgorithm? Or do you mean that this Thread must be implemented inside the method runAlgorithm? – Klausos Klausos Apr 28 '13 at 17:14
  • you can place the algorithm.runAlgorithm in loop inside this loop so that so want to iterate it and you may use if-else if in order to run it for a specific number if time say if i=1 ->call algorithm.runAlgorithm again use if i=2...i=5 call algorithm.runAlgorithm it will work for 5 times and set the sleep method so that it takes time the sleep can be use with if aslo – Tech Nerd Apr 28 '13 at 17:19
0
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);
    }
}

}

Tech Nerd
  • 822
  • 1
  • 13
  • 39
-1

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

See Here For more info on that!

Community
  • 1
  • 1
Synposis
  • 19
  • 1
  • 6
  • Answers should be more substantive than basically just a link to other articles or StackOverflow answers. If you discover than another SO question+answer describes the same situation as a question, please post a link to it as a comment on the question. – Jonathon Reinhart Jul 08 '13 at 04:34