| 1 |
// file: "tfib.java" |
| 2 |
|
| 3 |
class FibThread extends Thread |
| 4 |
{ |
| 5 |
FibThread (int arg) { n = arg; } |
| 6 |
|
| 7 |
public void run () |
| 8 |
{ try { result = fib (n); } catch (InterruptedException e) { } } |
| 9 |
|
| 10 |
private int n; |
| 11 |
|
| 12 |
public int result; |
| 13 |
|
| 14 |
private int fib (int n) |
| 15 |
throws InterruptedException |
| 16 |
{ |
| 17 |
if (n < 2) |
| 18 |
return 1; |
| 19 |
else |
| 20 |
{ |
| 21 |
FibThread x = new FibThread (n-2); |
| 22 |
x.start (); |
| 23 |
int y = fib (n-1); |
| 24 |
x.join (); |
| 25 |
return x.result + y; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
class tfib |
| 31 |
{ |
| 32 |
public static void main (String[] args) |
| 33 |
throws InterruptedException |
| 34 |
{ |
| 35 |
int n = 14; // Integer.parseInt (args[0]); |
| 36 |
int repeat = 100; //Integer.parseInt (args[1]); |
| 37 |
FibThread x; |
| 38 |
|
| 39 |
do |
| 40 |
{ |
| 41 |
x = new FibThread (n); |
| 42 |
x.start (); |
| 43 |
x.join (); |
| 44 |
repeat--; |
| 45 |
} while (repeat > 0); |
| 46 |
|
| 47 |
System.out.println (x.result); |
| 48 |
} |
| 49 |
} |