13

I have a method, which writes to the database. The requirement is to make sure that this method does not execute after a certain time elapses. If it returns before that, then nothing should be done.

The one basic approach that I can think of is doing something like this.

public class LimitedRuntime {

    public static void writeToDb(){
            // writes to the database
    }

    public static void main(String[] args) {
        long totalExecutionTime = 8000L;
        long startTime = System.currentTimeMillis();

        while(System.currentTimeMillis() - startTime < totalExecutionTime )
        {
            writeToDb();
        }   
    }
}

One problem with this approach is that even if the method returns before the max total execution time, even then the program halts so as to wait for the time to elapse.

How can I do this better (or maybe more correctly) ? And if we use Thread, how can we find out which Thread executes that method ?

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
OneMoreError
  • 7,518
  • 20
  • 73
  • 112

2 Answers2

20

You can do this by sending your job to an executor:

 public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(4);

    Future<?> future = executor.submit(new Runnable() {
        @Override
        public void run() {
            writeToDb();            //        <-- your job
        }
    });

    executor.shutdown();            //        <-- reject all further submissions

    try {
        future.get(8, TimeUnit.SECONDS);  //     <-- wait 8 seconds to finish
    } catch (InterruptedException e) {    //     <-- possible error cases
        System.out.println("job was interrupted");
    } catch (ExecutionException e) {
        System.out.println("caught exception: " + e.getCause());
    } catch (TimeoutException e) {
        future.cancel(true);              //     <-- interrupt the job
        System.out.println("timeout");
    }

    // wait all unfinished tasks for 2 sec
    if(!executor.awaitTermination(2, TimeUnit.SECONDS)){
        // force them to quit by interrupting
        executor.shutdownNow();
    }
}
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • 1
    When interrupting Thread, you have to remember, that you have to add a condition like `if (isInterrupted()) { return; }`. Without this, the Thread won't be interrupted at all! Don't know how it will work with the Runnable interface... – bobbel Dec 10 '13 at 16:58
  • Yep, I assumed that DB driver should support this. – Andrey Chaschev Dec 10 '13 at 17:00
  • I'm not familiar with `ExecutorService`. I will read about it. Btw can you just tell me a) What happens above if the method completes and returns before the max time is elapsed ? b) I want to terminate the method instantaneously once the time limit exceeds. For that, should I use `executor.awaitTermination(0, TimeUnit.SECONDS)` ? – OneMoreError Dec 10 '13 at 17:05
  • a) Future stops awaiting the job to finish, `main()` thread continues execution. In this particular case app will simply quit. b) `future.cancel(true)` should terminate the thread. The thread needs to support termination as @bobbel noted. Your db driver should support it, if thread does multiple requests, it should check `Thread.interrupted()` flag between them. – Andrey Chaschev Dec 10 '13 at 17:25
3

There is also an AspectJ solution for that with jcabi-aspects library:

@Timeable(limit = 5, unit = TimeUnit.SECONDS)
public String writeToDb() {
  // writeToDb
}

There is an article explaining it further: Limit Java Method Execution Time

gvlasov
  • 18,638
  • 21
  • 74
  • 110