How to stop execution after a certain time in Java
We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this problem. We will also go through some of their difficulties.
Making Use of a Loop
Consider the case when we are process a batch of items in a loop, such as some product data in an e-commerce application, but we don't need to finish all of them.
In actuality, we had like to processing only up to a particular time, then halt the execution and display whatever the list has processing up to that time. Let's look at an example:
longstart= System.currentTimeMillis();
longend= start + 30 * 1000;
while (System.currentTimeMillis() < end)
{
// On the item, there was an expensive operation.
}
If the time limit of 30 seconds is exceeded, the loop will be broken. The following are some points in the above solution:
- Low precision: The loop can last longer than the time limit. The duration of each cycle will decide this. For instance, if each iteration takes up to 7 seconds, the overall time can reach 35 seconds, which is about 17% longer than the targeted time limit of 30 seconds.
- Blocking: Performing such processing in the main thread may not be a smart idea because it will cause the thread to be blocked for a lengthy time. These operations should instead be separated from the main thread.
In the following part, we will look at how the interrupt-based technique overcomes these drawbacks.
Employing a Disruption Mechanism
To perform the long-running processes, we will use a different thread. When the main thread timeouts, it will send an interrupt signal to the worker thread.
If the worker thread is still alive, the signal will be caught and the execution will be halted. The worker thread will not be affected if the worker completes before the timeout. Let's have a look at the thread for workers:
classLongRunningTaskimplementsRunnable
{
@Override
publicvoidrun()
{
for (intj=0; j< Long.MAX_VALUE; j++)
{
if(Thread.interrupted())
{
return;
}
}
}
}
The for loop goes through Long in the above code. MAX VALUE is a long-running operation simulator. Any other operation could be used in its place. Because not all operations are interruptible, it's critical to check the interrupt flag. As a result, we should manually verify the flag in those circumstances.
After each iteration, we should also check this flag to guarantee that the thread does not execute itself more than one iteration.
Following that, we'll look at three alternative ways to deliver an interrupt signal.
- Use the Timer: We can also create a TimerTask to disrupt the worker thread when it times out:
class TimeOutTask extends TimerTask
{
private Thread thread;
private Timer timer;
public TimeOutTask(Thread thread, Timer timer)
{
this.thread = thread;
this.timer = timer;
}
@Override
public void run()
{
if(thread != null && thread.isAlive())
{
thread.interrupt();
timer.cancel();
}
}
}
We have defined a TimerTask here that takes a worker thread when it is created. When the run method is called, it will interrupt the worker thread. After a three-second late, the Timer will start the TimerTask:
Thread thread = new Thread(new LongRunningTask());
thread.start();
Timer timer = new Timer();
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
timer.schedule(timeOutTask, 3000);
- Instead-of utilizing a Timer, we can utilize the get function of a Future:
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(new LongRunningTask());
try
{
future.get(7, TimeUnit.SECONDS);
}
catch (TimeoutException e)
{
future.cancel(true);
}
catch (Exception e)
{
// handle other exceptions
}
finally
{
executor.shutdownNow();
}
We used the ExecutorService to submit a worker thread that returns a Future instance, whose get function will block the main thread until the timer expires. After the provided timeout, it will throw a TimeoutException. By executing the cancel function on the Future object in the catch block, we disrupt the worker thread.
The key advantage of this solution over the previous one is that it manages the threads through a pool, whereas the Timer just employs a single thread (no pool).
- Use the ScheduledExcecutorSercvice: To disturb the task, we can utilise ScheduledExecutorService. This class is an expanded of an ExecutorService, and It has the same functionality as an ExecutorService with the addition of a few functions for scheduling execution. This can carry out the supplied task after a set amount of time has passed:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
Future future = executor.submit(new LongRunningTask());
Runnable cancelTask = () -> future.cancel(true);
executor.schedule(cancelTask, 3000, TimeUnit.MILLISECONDS);
executor.shutdown();
With the function newScheduledThreadPool, we established a scheduled thread pool of size 2. A Runnable, a late value, and the late unit are passed to the ScheduledExecutorService#schedule function.
The job in the preceding program is scheduled to run three seconds after it is submitted. The original long-running task will be canceled by this task.
It's worth noting that, in contrast to the preceding way, calling the Future#get function does not block the main thread. As a result, among all the ways listed above, it is the most popular.
Is There a Guarantee?
There is no guarantee that the execution will be halted once a specific amount of time has passed. Because not all blocking ways are interruptible, this is the fundamental reason. In truth, there are just a handful of well-defined interruptible ways. When a thread is disrupted and a flag is set, nothing else happens until the thread reaches one of these interruptible procedures.
Read and write functions, for instance, are only interruptible if they're called on streams established with an InterruptibleChannel. InterruptibleChannel is not the same as BufferedReader. As a result, if the thread is using it to read a file, interrupt() on the thread that is stalled in the read function has no effect.
In a loop, though, we can properly check for the interrupt flag after each read. This will ensure that the thread will be stopped with some delay. However, because we don't know how long a read operation would take, this does not guarantee that the thread will be stopped after a specific amount of time.
The Object class's wait function, on the other hand, is interruptible. As a result, after the interrupt flag is set, the thread stalled in the wait function will immediately issue an InterruptedException.
The blocked methods can be identified by checking for throws InterruptedException in their function definitions.
Avoid using the deprecated Thread.stop() function, as this is one of the most critical pieces of advice. When you stop the thread, it unlocks all of the monitors it has locked. The ThreadDeath exception propagates up the stack, causing this to happen.
The inconsistent objects become visible to other threads if any of the items formerly safeguarded by these monitors have become unreliable.This might lead to irrational behavior that is difficult to recognize and explain.
Interruption Design
The need of having interruptible ways to stop the execution as soon as feasible was discussed in the preceding section. As a result, from a design standpoint, our programming must take into account this expectation. Consider the following scenario:
We have a long-running task to do, and we need to make sure it doesn't take longer than expected. Assume that the task may be broken down into separate steps. Let's make a class for the steps in the task:
class Step
{
private static int MAX = Integer.MAX_VALUE/2;
int number;
public Step(int number)
{
this.number = number;
}
public void perform() throws InterruptedException
{
Random rnd = new Random();
int target = rnd.nextInt(MAX);
while (rnd.nextInt(MAX) != target) {
if (Thread.interrupted())
{
throw new InterruptedException();
}
}
}
}
On each iteration, the Step#perform method tries to obtain a target random integer while also asking for the flag. When the flag is set, the function throws an InterruptedException. Let's define the task that will carry out all of the steps:
public class SteppedTask implements Runnable
{
private List<Step> steps;
public SteppedTask(List<Step> steps)
{
this.steps = steps;
}
@Override
public void run()
{
for (Step step : steps)
{
try
{
step.perform();
}
catch (InterruptedException e)
{
// handle interruption exception
return;
}
}
}
}
The SteppedTask includes a set of steps to complete in the above code. Each step is performed using a for loop, which also handles the InterruptedException to terminate the process when it occurs. Finally, here's an example of how we may apply our interruptible task:
List<Step> steps = Stream.of(new Step(1),new Step(2),new Step(3),new Step(4)).collect(Collectors.toList());
Thread thread = new Thread(new SteppedTask(steps));
thread.start();
Timer timer = new Timer();
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
timer.schedule(timeOutTask, 10000);
First, we design a four-step SteppedTask. Second, we use a thread to do the operation. Finally, we use a timer and a timeout job to interrupt the thread after 10 seconds.
We can assure you that our long-running process can be interrupted while doing any step with this architecture. As previously stated, there is no guarantee that it will terminate at the exact time indicated, but it is certainly preferable to a non-interruptible task.