0

I have a Java code which is calling two methods of a class. Like following,

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());
  }
  }
}

In the first method, the code will create a shared array and in the second method it will compare it with a file.

Now, I want to run those two ReadData() methods in a parallel manner, using multiple threads. Can anybody help me do that. Possibly with some code modification.

5
  • 2
    Are you trying to run in parallel to work on two separate sets of data at once (to make better use of a multi-core processor, for instance) or are you trying to get two threads to work on a single set of data (which is much more troublesome)? Commented Jul 29, 2012 at 18:11
  • Which two? Do you mean createArray and ReadData? Or do you want to call ReadData multiple times with different arguments? In the latter case I would suggest to synchronize the access to your array arr. E.g. use a ReadWriteLock. Commented Jul 29, 2012 at 18:11
  • @user1515834, I want to use it to make better use of a multi-core processor. And as you can see no synchronicity is needed. Commented Jul 29, 2012 at 18:12
  • @coding.mof, I want to run ReadData() parallely to make it faster on multi-core processor. Commented Jul 29, 2012 at 18:14
  • Please indent your code for readability!! Commented Jul 29, 2012 at 18:25

3 Answers 3

2
import java.io.*;
public 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.createArray(0); //static method call to set the static arr variable
        Exampless ex1 = new Exampless("1.dat");
        Exampless ex2 = new Exampless("2.dat");
        Thread t1 = new Thread(ex1);
        Thread t2 = new Thread(ex2);
        t1.start(); //calls the run method in ex1 in a new thread
        t2.start();

    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}
}

class Exampless implements Runnable {

public static int[] arr = new int[100];
public String _name;

public Exampless(String name) {
    this._name = name;
}

static void createArray(int z) {
    for (int i = z; i < z + 100; i++) {
        arr[i - z] = i;
    }
}

@Override
public void run() {
    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());
    }
}

}
Sign up to request clarification or add additional context in comments.

Comments

0

Make the class implement "runnable", create 2 instances and run them.

1 Comment

But, if I make the class implement "runnable", then it will run both two methods parallel. I want only one to run in parallel.
0

As long as you don't modify your array during calling ReadData in a different thread you could leave it without synchronization.
A better and more robust way would be to synchronize the access to your array.

import java.io.*;
class Exampless {

    private static int[] arr = new int[100];
    private static ReadWriteLock lock = new ReentrantReadWriteLock();

    void createArray(int z) {
        lock.writeLock().lock();
        try {
        for (int i = z; i < z + 100; i++)
            arr[i - z] = i;
        } finally {
            lock.writeLock().unlock();
        }
    }

    void ReadData(String name) {
        lock.readLock().lock();
        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());
        } finally {
            lock.readLock().unlock();
        }
    }
}

1 Comment

You still need to create your Threads but you can safely invoke your ReadData method multiple times by different threads. Without synchonization it would come to Race Conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.