Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Question about recursion and threads

I'm writing a program that requires the answer to use recursion, however, I want to use a Thread so I can see it working.
How do I pause each iteration of the recursion loop using a Thread?
[203 byte] By [srekcus] at [2007-11-11 8:01:58]
# 1 Re: Question about recursion and threads
Recursion is when you have a method, and inside that method you call itself. Here's an example of how to get a specific index of the fibonacci sequence:
public int fib(int n) {
/* 0th and 1st indexes are 1 */
if (n == 0 || n == 1) {
return 1;
} else {
/* an index of the fibonacci sequence is the sum of the two previous terms */
return fib(n - 1) + fib(n - 2);
}
}
destin at 2007-11-11 22:36:34 >
# 2 Re: Question about recursion and threads
Thanks, but how do I pause each iteration of the recursion loop using a thread?
srekcus at 2007-11-11 22:37:40 >
# 3 Re: Question about recursion and threads
:confused:
srekcus at 2007-11-11 22:38:44 >
# 4 Re: Question about recursion and threads
Just put Thread.sleep(millisecondsToSleep); in the beginning of the method.
destin at 2007-11-11 22:39:39 >
# 5 Re: Question about recursion and threads
Thanks, but how do I pause each iteration of the recursion loop using a thread?

Here's a simple program that prints 0 to 100 and pauses after each number:

public class Test
{
public static void main(String[] args) throws Exception
{
for(int x = 0; x < 100; x++)
{
System.out.println(x);
Thread.sleep(1000);
}
}
}

Thread.sleep(long millis) is what makes it pause, move this where you want your code to pause and it should work. Hope this helps.

Note that Thread.sleep() throws an InterruptedException.
Nathan87 at 2007-11-11 22:40:43 >
# 6 Re: Question about recursion and threads
Cool, thanks!
srekcus at 2007-11-11 22:41:42 >