I am trying to stop a long running method after 10 seconds of execution, so far i followed the timer instructions on baeldung.

<https://www.baeldung.com/java-stop-execution-after-certain-time#1-using-a-timer>

When the method is a simple call to a thread sleep it works, but when I call my function with sub methods it doesn't stop.

My implementation looks like this:

```java
 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();
            }
        }
    }
    class Execution implements Runnable {

        private String carpeta;
        private Experiment exp;

        public Execution(String carpeta, Experiment exp) {
            this.carpeta = carpeta;
            this.exp = exp;
        }

        @Override
        public void run() {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                   exp.executeExperiment(carpeta);
                }
            } catch (InterruptedException e) {
                System.out.println("Fin de ejecución por tiempo");
            }
        }
    }

```

And the way I am calling this execution is throught the executeTimedExperiment method

```java
    public Experiment() {
        this.cases = new ArrayList<>();
    }

    private void executeTimedExperiment(String carpeta){
        Thread t = new Thread(new Execution(carpeta,this));
        Timer timer = new Timer();
        timer.schedule(new TimeOutTask(t, timer), 10000);
        t.start();
    }

    private void executeExperiment(String carpeta) throws InterruptedException {

        String[] files = getFiles(carpeta);
        Arrays.sort(files);

        for (String file : files) {
             executeCase(carpeta, file);
        }

    }

    private boolean executeCase(String carpeta, String file) {

        Graph g = readDataToGraph(carpeta + "/" + file);
        Solution s = new ExactSolutionGenerator().ExactSolution(g);
        addNewCase(file, s);

    }

```

The executeExperiment method is the long running and I marked it with InterruptedException but the compiler tells me the exception is never throw.

What happens now when I execute it is that it runs normally without stoppping.

I am not sure if I need to add InterruptedException to the submethods or something else, but I would like to not touch the submethods if possible.

Thanks in advance.