eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

eBook – Java Concurrency – NPI (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

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() {
        for (int i = 0; i < Long.MAX_VALUE; i++) {
            if(Thread.interrupted()) {
                return;
            }
        }
    }
}

Here, the for loop through Long.MAX_VALUE 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 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();
        }
    }
}

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 a three seconds delay:

Thread thread = new Thread(new LongRunningTask());
thread.start();

Timer timer = new Timer();
TimeOutTask timeOutTask = new TimeOutTask(thread, timer);
timer.schedule(timeOutTask, 3000);

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 {
    future.get(7, TimeUnit.SECONDS);
} catch (TimeoutException e) {
    future.cancel(true);
} catch (Exception e) {
    // handle other exceptions
} finally {
    executor.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 ScheduledExcecutorService

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());
Runnable cancelTask = () -> future.cancel(true);

executor.schedule(cancelTask, 3000, 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 three seconds 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. Design for Interruption

In the previous section, we highlighted the importance of having interruptible methods to stop the execution as soon as possible. Therefore, our code needs to consider this expectation from a design perspective.

Imagine we have a long-running task to execute, and we need to make sure it doesn’t take more time than the specified. Also, suppose the task can be split into individual steps.

Let’s create a class for the task steps:

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

Here, the Step#perform method tries to find a target random integer while asking for the flag on each iteration. The method throws an InterruptedException when the flag is activated.

Now, let’s define the task which will be performing all 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;
            }
        }
    }
}

Here, the SteppedTask has a list of steps to execute. A for loop performs each step and handles the InterruptedException for stopping the task when it occurs.

Finally, let’s see an example of using 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 create a SteppedTask with four steps. Second, we run the task using a thread. Last, we interrupt the thread after ten seconds using a timer and a timeout task.

With this design, we can ensure our long-running task can be interrupted while executing any step. As we have seen before, the downside is there is no guarantee it will stop at the exact time specified, but surely better than a non-interruptible task.

6. 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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook – Java Concurrency – NPI (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook Jackson – NPI EA – 3 (cat = Jackson)