Question: I have tried to implement timeout in the code. Basically I want to start the timeout and if timeout occurs it should call a method.

```
Pseudocode way:
      in bar function
            start timeout    ---> ( at background, if timeout occurs call foo() )

      in foo function
            cancel timeout
            print something

```

How to call a method after timeout occurs?

EDIT: I should have ability to **re-start** the timeout.

Answer: 
There are probably a lot of different ways to achieve it...

The simplest might be just to use [`java.util.Timer`](http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) which will allow you to schedule a `TimerTask` to be executed at some time in the future...

You could also take a look at [ScheduledExecutorService](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html) which I believe is it's `Executor` equivalent.
Perhaps this might answer your question 

```
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Test {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> future = executor.submit(new Task());

        try {
            System.out.println("Started..");
            System.out.println(future.get(3, TimeUnit.SECONDS));
            System.out.println("Finished!");
        } catch (TimeoutException e) {
            System.out.println("Terminated!");
        }

        executor.shutdownNow();
    }
}

class Task implements Callable<String> {
    @Override
    public String call() throws Exception {
        Thread.sleep(5000); //It means 5 seconds
        return "Ready!";
    }
}

```

If you have question please referring to the [threads](http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html)

Explanation of the future : 
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. 

Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled. If you would like to use a Future for the sake of cancellability but not provide a usable result, you can declare types of the form Future and return null as a result of the underlying task. 

Please referring to this [site](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html)