forked from disha2sinha/Object-Oriented-Programming-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeAndFiboThread.java
81 lines (76 loc) · 2.21 KB
/
PrimeAndFiboThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
class fibonacci extends Thread{
int a=0,b=1,n;
fibonacci(int n){
super("FIBONACCI THREAD");
System.out.println("Fibonacci Thread in control");
this.n=n;
start();
}
public void run()
{
try{
for(int i=1;i<=n;i++)
{
int temp=a+b;
System.out.println("Fibonacci term:"+temp);
a=b;
b=temp;
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + "interrupted");
}
System.out.println("Exiting"+ Thread.currentThread().getName());
}
}
class prime extends Thread {
int n;
prime(int n) {
super("PRIME THREAD");
System.out.println("Prime Thread in control");
this.n = n;
start();
}
public void run() {
try {
for (int i = 1; i <=n; i++) {
int flag=0;
for(int j=2;j<n;j++){
if (i%j==0){
flag=1;
break;
}
}
if(flag==1){
System.out.println("prime term:" + i);
Thread.sleep(500);
}
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "interrupted");
}
System.out.println("Exiting" + Thread.currentThread().getName());
}
}
/**
* PrimeAndFiboThread
*/
class PrimeAndFiboThread {
public static void main(String[] args) throws IOException{
Thread.currentThread().setPriority(10);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number of terms:");
int n=Integer.parseInt(br.readLine());
fibonacci f=new fibonacci(n);
prime p=new prime(n);
try {
f.join();
p.join();
} catch (Exception e) {
System.out.println("Exception Caught"+e);
}
}
}