I am trying to write a search (ids) program in java that halts if 3 minutes have passed and a solution has not been found. I tried using a while loop, but the loop continued even once the solution had been found but the time limit was still going, so I changed it to using an Executor service, but that also keeps repeating. I don't think I have the Executor working right. What am I doing wrong??
public void search(Node head) throws InterruptedException, ExecutionException{
    solution = new Stack<Node>();
    allnodes = 0;
    ExecutorService es = Executors.newSingleThreadExecutor();
    Future f = es.submit(new Runnable() {
        @Override
        public void run() {
            startTime = System.nanoTime();
            while(!Thread.interrupted()) {
                endTime = searching(start);
            }
            long time = endTime - startTime;
            System.out.printf("Finished task after %,d ns%n"+ time);
        }
    });
    try {// stops if the task completes.
        f.get(1, TimeUnit.SECONDS); 
    } catch (TimeoutException e) {
        f.cancel(true);
    }
    es.shutdown();
}
public long searching(Node head){
    solution.push(head);
        if((head.getRow() == goal.getRow()) && (head.getCol() == goal.getCol())){
            System.out.println("solution");
            endTime = System.currentTimeMillis();
            solution.push(head);
            allnodes = allnodes+1;
            return endTime; //here it returns the end time to then be printed out but the loop keeps going?
        }
        //recursively calling
        int r = head.getRow();
        int c = head.getCol();
        if((r-1 >-1 )){
            if(gr.getstate(r - 1, c) == State.Unvisited){       
                allnodes = allnodes+1;
                System.out.println("go north");
                gr.updatemap(r - 1, c);
                solution.push(head);
                searching(head.getN(head));
            }
        }
        if(r+1 < (map[1].length-1)){
            if(gr.getstate(r + 1, c) == State.Unvisited){               
                System.out.println("go south");
                allnodes = allnodes+1;
                gr.updatemap(r + 1, c);
                solution.push(head);
                head = head.getS(head);
                searching(head);    
            }
        }
        if(c+1 < map.length-1){
            if(gr.getstate(r , c +1) == State.Unvisited){
                allnodes = allnodes+1;
                System.out.println("go east");
                gr.updatemap(r , c +1);
                solution.push(head);
                head = head.getE(head);
                searching(head);
            }   
        }
        if(c -1 > -1){
            if (gr.getstate(r, c-1) == State.Unvisited){
                System.out.println("go west");
                allnodes = allnodes+1;
                gr.updatemap(r, c-1);
                solution.push(head);
                head = head.getW(head);
                searching(head);
            }   
        }
        return endTime;
}