Thinking in Java - Fibonacci and Runnable

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rankgo/article/details/80335992



/*遵循generic/Fibonacci.java的形式,创建一个任务,它可以有n个费波纳奇数字组成的序列,其中

n是通过任务的构造器而提供的,使用线程创建大量的这种任务并驱动它们。
费波纳奇数列,具体数列为:1,1,2,3,5,8,13,21,34,55,89,144,233,……*/


public class Fibonacci implements Runnable
{
private static int taskCount = 0;
private final int id = taskCount++;
private int count = 0;
Fibonacci(int n){
this.count = n;
}

private int fib(int n) {
if(n < 2) {
return 1;
}

return fib(n - 2) + fib(n - 1); 
}

public void run() {
for(int i = 0; i < count; i++) {
System.out.println("#" + id + " : " + fib(i));
Thread.yield();
}
}

public static void main(String[] args) {
Thread t1 = new Thread(new Fibonacci(7));
t1.start();

Thread t2 = new Thread(new Fibonacci(7));
t2.start();

Thread t3 = new Thread(new Fibonacci(7));
t3.start();
}
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页