java - Does Thread.sleep() prevent a StackOverflow error? -
would following code throw stackoverflow error?
public class teststackoverflow { public static void main(string args[]) { recursion(); } public static void recursion() { try { thread.sleep(100); } catch(exception err) {} recursion(); } } it seems thread.sleep(100); prevents stackoverflow error wondering if overtime stackoverlow error thrown? , why slowing recursion prevent it? guess unsure of stackoverflow , why seems stop it?
thread.sleep cannot prevent stackoverflowerror. doesn't take stack frames off stack. postpone error. each method calls takes stack memory. making recursive call without stopping, program take more stack memory each call, filling , causing error.
taking out thread.sleep produces error immediately.
changing thread.sleep(1); (sleep 1 ms @ time) makes program take 11 seconds produce error on machine.
Comments
Post a Comment