Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Thread[] threads = new Thread[10];
- for ( int i = 0; i<10; i++)
- {
- threads[i] = new Thread(new Fib(5*i,5*i+5),"Thread" + i);
- }
- for (Thread thread : threads)
- {
- thread.start();
- try
- {
- thread.join(); // wait for the threads to terminate
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- //System.out.println("Thread "+ fib.getName() + " started.");
- System.out.println();
- }
- }
- class Fib implements Runnable
- {
- long a,b,c,n,start,end;
- Fib(int start, int end)
- {
- a=help(start);
- c=n=0;
- b=help(start+1);
- this.start = start;
- this.end = end;
- }
- public void run()
- {
- for(long i = start; i<=end; i++)
- {
- System.out.println(n+"th" +" Fib no: = "+a);
- c=a+b;
- a=b;
- b=c;
- }
- }
- public int help(int n)
- {
- int a = 0, b = 1, c;
- if (n == 0)
- return a;
- for (int i = 2; i <= n; i++) {
- c = a + b;
- a = b;
- b = c;
- }
- return b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement