Advertisement
Guest User

1

a guest
Nov 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1.  
  2. public class Main {
  3.  
  4. public static void main(String[] args) {
  5. Thread[] threads = new Thread[10];
  6. for ( int i = 0; i<10; i++)
  7. {
  8. threads[i] = new Thread(new Fib(5*i,5*i+5),"Thread" + i);
  9. }
  10. for (Thread thread : threads)
  11. {
  12. thread.start();
  13. try
  14. {
  15. thread.join(); // wait for the threads to terminate
  16. }
  17. catch (InterruptedException e)
  18. {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. //System.out.println("Thread "+ fib.getName() + " started.");
  24. System.out.println();
  25. }
  26.  
  27. }
  28.  
  29.  
  30.  
  31.  
  32. class Fib implements Runnable
  33. {
  34. long a,b,c,n,start,end;
  35.  
  36. Fib(int start, int end)
  37. {
  38. a=help(start);
  39. c=n=0;
  40. b=help(start+1);
  41. this.start = start;
  42. this.end = end;
  43. }
  44. public void run()
  45. {
  46. for(long i = start; i<=end; i++)
  47. {
  48. System.out.println(n+"th" +" Fib no: = "+a);
  49. c=a+b;
  50. a=b;
  51. b=c;
  52. }
  53. }
  54. public int help(int n)
  55. {
  56. int a = 0, b = 1, c;
  57. if (n == 0)
  58. return a;
  59. for (int i = 2; i <= n; i++) {
  60. c = a + b;
  61. a = b;
  62. b = c;
  63. }
  64. return b;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement