Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this article, we'll learn how we can end a long-running execution after a certain time. We'll explore the various solutions to this problem. Also, we'll cover some of their pitfalls.

2. Using a Loop

Imagine that we're processing a bunch of items in a loop, such as some details of the product items in an e-commerce application, but that it may not be necessary to complete all the items.

In fact, we'd want to process only up to a certain time, and after that, we want to stop the execution and show whatever the list has processed up to that time.

Let's see a quick example:

long start = System.currentTimeMillis();
long end = start + 30*1000;
while (System.currentTimeMillis() < end) {
    // Some expensive operation on the item. 
}

Here, the loop will break if the time has surpassed the limit of 30 seconds. There are some noteworthy points in the above solution:

  • Low accuracy: The loop can run for longer than the imposed time limit. This will depend on the time each iteration may take. For example, if each iteration may take up to 7 seconds, then the total time can go up to 35 seconds, which is around 17% longer than the desired time limit of 30 seconds
  • Blocking: Such processing in the main thread may not be a good idea as it'll block it for a long time. Instead, these operations should be decoupled from the main thread

In the next section, we'll discuss how the interrupt-based approach eliminates these limitations.

3. Using an Interrupt Mechanism

Here, we'll use a separate thread to perform the long-running operations. The main thread will send an interrupt signal to the worker thread on timeout.

If the worker thread is still alive, it'll catch the signal and stop its execution. If the worker finishes before the timeout, it'll have no impact on the worker thread.

Let's take a look at the worker thread:

class LongRunningTask implements Runnable {
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            // log error
        }
    }
}

Here, Thread.sleep simulates a long-running operation. Instead of this, there could be any other operation. It's important to check the interrupt flag because not all the operations are interruptible. So in those cases, we should manually check the flag.

Also, we should check this flag in every iteration to ensure that the thread stops executing itself within the delay of one iteration at most.

Next, we'll cover three different mechanisms of sending the interrupt signal.

3.1. Using a Timer

Alternatively, we can create a TimerTask to interrupt the worker thread upon timeout:

class TimeOutTask extends TimerTask {
    private Thread t;
    private Timer timer;

    TimeOutTask(Thread t, Timer timer){
        this.t = t;
        this.timer = timer;
    }
 
    public void run() {
        if (t != null && t.isAlive()) {
            t.interrupt();
            timer.cancel();
        }
    }
}

Here, we've defined a TimerTask that takes a worker thread at the time of its creation. It'll interrupt the worker thread upon the invocation of its run method. The Timer will trigger the TimerTask after the specified delay:

Thread t = new Thread(new LongRunningTask());
Timer timer = new Timer();
timer.schedule(new TimeOutTask(t, timer), 30*1000);
t.start();

3.2. Using the Method Future#get

We can also use the get method of a Future instead of using a Timer:

ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(new LongRunningTask());
try {
    f.get(30, TimeUnit.SECONDS);
} catch (TimeoutException e) {
    f.cancel(true);
} finally {
    service.shutdownNow();
}

Here, we used the ExecutorService to submit the worker thread that returns an instance of Future, whose get method will block the main thread until the specified time. It'll raise a TimeoutException after the specified timeout. In the catch block, we are interrupting the worker thread by calling the cancel method on the Future object.

The main benefit of this approach over the previous one is that it uses a pool to manage the thread, while the Timer uses only a single thread (no pool).

3.3. Using a ScheduledExcecutorSercvice

We can also use ScheduledExecutorService to interrupt the task. This class is an extension of an ExecutorService and provides the same functionality with the addition of several methods that deal with the scheduling of execution. This can execute the given task after a certain delay of set time units:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
Future future = executor.submit(new LongRunningTask());
executor.schedule(new Runnable(){
    public void run(){
        future.cancel(true);
    }
}, 1000, TimeUnit.MILLISECONDS);
executor.shutdown();

Here, we created a scheduled thread pool of size two with the method newScheduledThreadPool. The ScheduledExecutorService#schedule method takes a Runnable, a delay value, and the unit of the delay.

The above program schedules the task to execute after one second from the time of submission. This task will cancel the original long-running task.

Note that unlike the previous approach, we are not blocking the main thread by calling the Future#get method. Therefore, it's the most preferred approach among all the above-mentioned approaches.

4. Is There a Guarantee?

There's no guarantee that the execution is stopped after a certain time. The main reason is that not all blocking methods are interruptible. In fact, there are only a few well-defined methods that are interruptible. So, if a thread is interrupted and a flag is set, nothing else will happen until it reaches one of these interruptible methods.

For example, read and write methods are interruptible only if they're invoked on streams created with an InterruptibleChannel. BufferedReader is not an InterruptibleChannel. So, if the thread uses it to read a file, calling interrupt() on this thread blocked in the read method has no effect.

However, we can explicitly check for the interrupt flag after every read in a loop. This will give a reasonable surety to stop the thread with some delay. But, this doesn't guarantee to stop the thread after a strict time, because we don't know how much time a read operation can take.

On the other hand, the wait method of the Object class is interruptible. Thus, the thread blocked in the wait method will immediately throw an InterruptedException after the interrupt flag is set.

We can identify the blocking methods by looking for a throws InterruptedException in their method signatures.

One important piece of advice is to avoid using the deprecated Thread.stop() method. Stopping the thread causes it to unlock all of the monitors that it has locked. This happens because of the ThreadDeath exception that propagates up the stack.

If any of the objects previously protected by these monitors were in an inconsistent state, the inconsistent objects become visible to other threads. This can lead to arbitrary behavior that is very hard to detect and reason about.

5. Conclusion

In this tutorial, we've learned various techniques for stopping the execution after a given time, along with the pros and cons of each. The complete source code can be found over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
guest
0 Comments
Inline Feedbacks
View all comments