クラスの2つのメソッドを呼び出すJavaコードがあります。次のように、
import java.io.*;
class Example
{
public static void main(String args[])
{
try{
FileOutputStream fos = new FileOutputStream("1.dat");
DataOutputStream dos = new DataOutputStream(fos);
for(int i =0 ; i < 100 ; i++){
dos.writeInt(i);
}
dos.close();
FileOutputStream fos1 = new FileOutputStream("2.dat");
DataOutputStream dos1 = new DataOutputStream(fos1);
for(int i =100 ; i < 200 ; i++){
dos1.writeInt(i);
}
dos1.close();
Exampless ex = new Exampless();
ex.createArray(0);
ex.ReadData("1.dat");
ex.ReadData("2.dat");
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
class Exampless{
public static int []arr = new int [100] ;
void createArray(int z){
for(int i =z ; i < z+100 ; i++)
arr[i-z] = i ;
}
public synchronized void ReadData(String name){
try{
int cnt = 0;
FileInputStream fin = new FileInputStream(name);
DataInputStream din = new DataInputStream(fin);
for(int i = 0 ; i < 100 ; i++){
int c = din.readInt();
if(c == arr[i])
cnt++ ;
}
System.out.println("File name: " + name + " No. of Matches: " + cnt) ;
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}
最初の方法では、コードは共有配列を作成し、2番目の方法ではそれをファイルと比較します。
ReadData()
ここで、複数のスレッドを使用して、これら2つのメソッドを並行して実行したいと思います。誰かが私がそれをするのを手伝ってくれる?おそらくいくつかのコード変更があります。