Java: comment éviter StackOverflowException

Je suis en train d'écrire une fonction qui va s'appeler lui-même jusqu'à près de 5000 fois. Bien sûr, je reçois un StackOverflowException. Est-il possible que je peux réécrire ce code dans une manière assez simple?:

void checkBlocks(Block b, int amm) {

    //Stuff that might issue a return call

    Block blockDown = (Block) b.getRelative(BlockFace.DOWN);
    if (condition) 
        checkBlocks(blockDown, amm);


    Block blockUp = (Block) b.getRelative(BlockFace.UP);
    if (condition) 
        checkBlocks(blockUp, amm);

    //Same code 4 more times for each side

}

En passant, qu'est-ce que la limitation de la profondeur qu'on peut appeler les fonctions?

Merci!

source d'informationauteur Henrik Karlsson