-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeFile_File_Multithread.java
135 lines (124 loc) · 3.36 KB
/
PrimeFile_File_Multithread.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package multi_Thread_fileHandling;
import java.io.*;
class Prime {
String file1="D:\\jv prac\\class question\\src\\filep1.txt";
String file2="D:\\jv prac\\class question\\src\\filep2.txt";
synchronized void findprime1to100() { //finding prime
int a=1,b=100;
System.out.println("~ Finding prime numbers between 1 to 100.");
try(DataOutputStream dout=new DataOutputStream(new FileOutputStream(file1))){ //writing in file
for (int j=a;j<=b;j++) {
int flag = 0;
for (int i=1;i<=j;i++) {
if(j%i==0) {
flag++;
}
}
if(flag==2) {
//System.out.println(j);
String s=String.valueOf(j)+" ";
dout.write(s.getBytes());
}
else {continue;}
}
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
e.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
synchronized void findprime101to200() { //finding prime
int a=101,b=200;
System.out.println("~ Finding prime numbers between 101 to 200.");
try(DataOutputStream dout=new DataOutputStream(new FileOutputStream(file2))){ //writing in file
for (int j=a;j<=b;j++) {
int flag = 0;
for (int i=1;i<=j;i++) {
if(j%i==0) {
flag++;
}
}
if(flag==2) {
//System.out.println(j);
String s=String.valueOf(j)+" ";
dout.write(s.getBytes());
}
else {continue;}
}
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
e.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
synchronized void display1() {
try{
System.out.println("\n[+] Reading "+file1);
File file = new File(file1);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.print(st);
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
e.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
synchronized void display2() {
try {
System.out.println("\n[+] Reading "+file2);
File file = new File(file2);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.print(st);
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
e.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
public class PrimeFile_File_Multithread {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.out.println("*************** Hello 18BCI0174 - Aryan ****************");
Prime p=new Prime(); //1obj 2 thread
// 2object 4 thread -->synchronization needed
Thread t1=new Thread() {
public void run() {p.findprime1to100();}
};
Thread t2=new Thread() {
public void run() {p.findprime101to200();}
};
Thread t3=new Thread() {
public void run() {p.display1();}
};
Thread t4=new Thread() {
public void run() {p.display2();}
};
t1.start();
t2.start();
t1.join();
t2.join();
t3.start();
t4.start();
}
}