I have a method that returns a String.
Is it possible that after a certain time, if threshold is excedeed for that method, to return some specific string?
I did something similar in the past when spawning an external process with Runtime.getRuntime().exec(command). I think you could do something like this within your method:
Timer timer = new Timer(true);
InterruptTimerTask interruptTimerTask = new InterruptTimerTask(Thread.currentThread());
timer.schedule(interruptTimerTask, waitTimeout);
try {
    // put here the portion of code that may take more than "waitTimeout"
}
catch (InterruptedException e) {
    log.error("timeout exeeded");
}
finally {
    timer.cancel();
}
and here is InterruptTimerTask
/*
 * A TimerTask that interrupts the specified thread when run.
 */
protected class InterruptTimerTask extends TimerTask {
    private Thread theTread;
    
    public InterruptTimerTask(Thread theTread) {
        this.theTread = theTread;
    }
    @Override
    public void run() {
        theTread.interrupt();
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With