Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a maximum execution time for a method/thread

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 ?

like image 931
OneMoreError Avatar asked Dec 10 '13 16:12

OneMoreError


People also ask

How can we pause the execution of a thread for specific time?

Thread. sleep() method can be used to pause the execution of current thread for specified time in milliseconds.

How do you call a function repeatedly after a fixed time interval in Java?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

What happens to a thread after execution?

Thread is dead itself after its run method returns. It might still be in the heap, but it will not have its own stack anymore, and not do anything.


2 Answers

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();
    }
}
like image 153
Andrey Chaschev Avatar answered Oct 26 '22 23:10

Andrey Chaschev


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

like image 43
gvlasov Avatar answered Oct 27 '22 01:10

gvlasov