Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296653

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:06:24+00:00 2026-06-08T15:06:24+00:00

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

  • 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.

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:06:28+00:00Added an answer on June 8, 2026 at 3:06 pm
    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());
        }
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code which looks roughly like this: String urlString = ftp://polar.ncep.noaa.gov/pub/history/waves/multi_1.glo_30m.dp.200601.grb2;
I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int
I have some code which I think should compile, but doesn't: import java.io.InputStream; import
I have implement the following code to create a SSL server socket. public void
I have two libraries (.so), which I load in java code. However there are
Right now in some Java code I have something like this class A {
I have the following code in Perl which posts to my HTTP Servlet, calling
In my source tree have Java code which is automatically generated from XSD files.
I have a java code which gets the function names, their return types, their
I have some Java code which performs bitwise operations on a BitSet. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.