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