Skip to content

Conversation

@andrecsilva
Copy link

No description provided.

@andrecsilva andrecsilva force-pushed the ISS-2495/timed-execute branch from a7280e8 to 5264118 Compare December 17, 2024 12:02
@andrecsilva andrecsilva force-pushed the ISS-2495/timed-execute branch 2 times, most recently from f92acc0 to 4f57aa9 Compare December 17, 2024 12:20
@andrecsilva andrecsilva force-pushed the ISS-2495/timed-execute branch from 4f57aa9 to 91c0c21 Compare December 17, 2024 12:23
@andrecsilva andrecsilva changed the title Added utility to execute functions within timeout Added utility to execute functions within a time limit Dec 17, 2024
@andrecsilva andrecsilva marked this pull request as ready for review December 17, 2024 12:26
@andrecsilva andrecsilva requested a review from nahsra December 17, 2024 12:27
Copy link
Contributor

@nahsra nahsra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few issues:

  1. I think this library shouldn't offer what should be available in other libraries, like Guava, which offers lots of execute-with-timeout options. There are probably many others that feel more idiomatic to developers.

  2. I believe this code won't work with Java 8, which this project requires.

@nahsra
Copy link
Contributor

nahsra commented Dec 18, 2024

For your needs, consider Guava's SimpleTimeLimiter:

import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import java.util.concurrent.TimeUnit;

public class GuavaTimeoutExample {
    public static void main(String[] args) {
        TimeLimiter timeLimiter = SimpleTimeLimiter.create();

        try {
            String result = timeLimiter.callWithTimeout(() -> {
                // long-running operation
                return "task completed";
            }, 1, TimeUnit.SECONDS);

            System.out.println("Result: " + result);
        } catch (Exception e) {
            System.out.println("Task timed out or failed!");
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants