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 8301931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:09:43+00:00 2026-06-08T17:09:43+00:00

I have following Java code: import java.io.*; class Global{ public static int a =

  • 0

I have following Java code:

import java.io.*;
class Global{
public static int a = 0 ;
public static int b = 0 ;
}
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 < 20000; i++) {
            dos.writeInt(i);
        }
        dos.close();

        FileOutputStream fos1 = new FileOutputStream("2.dat");
        DataOutputStream dos1 = new DataOutputStream(fos1);

        for (int i = 20000; i < 40000; i++) {
            dos1.writeInt(i);
        }
        dos1.close();

        Exampless.createArray(20000); //static method call to set the static arr variable
        Exampless ex1 = new Exampless("1.dat"); //found number of matches in file
        Exampless ex2 = new Exampless("2.dat");
        Thread t1 = new Thread(ex1);
        Thread t2 = new Thread(ex2);
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        System.out.println("No. of Matches: " + (Global.a + Global.b ));

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

class Exampless implements Runnable {

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

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

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

public void run() {
    try {
        int cnt = 0;
        FileInputStream fin = new FileInputStream(_name);
        DataInputStream din = new DataInputStream(fin);
        for (int i = 0; i < 20000; i++) {
            int c = din.readInt();
            if (c == arr[i]) {
                cnt++;
            }
        }
        System.out.println("File name: " + _name + " No. of Matches: " + cnt);
        if(_name == "1.dat")
            Global.a = cnt ;
        else if(_name == "2.dat")
            Global.b = cnt ;
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}

}

There I am trying to run Exampless classes run method parallely. However, I want to make main thread wait until child thread finishes, which I have done using join. But, it makes execution of thread one after another. Can anybody help me how to solve this ? Another point is that, I want to share a variable (named as cnt which found number of matches in files)among two threads which I have done using Global class to found total number of matches. Is there any other nice solution ?

  • 1 1 Answer
  • 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-08T17:09:44+00:00Added an answer on June 8, 2026 at 5:09 pm

    t1.join() waits until t1 is done, so you need to invert the 2 lines:

    t1.start();
    t2.start();
    t1.join();
    t2.join();
    

    However, you would be better off using the high level concurrency package, and typically an ExecutorService:

    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(ex1);
    executor.submit(ex2);
    executor.shutdown();
    

    Your next question:

    I want to share a variable (named as cnt which found number of matches in files)among two threads which I have done using Global class to found total number of matches. Is there any other nice solution ?

    What you are doing is not thread safe, unless those variables are volatile. If they are not, it is possible that your print in your main will not print the latest value of those variables.

    A better solution would be to have Exampless implement Callable instead of Runnable, in which case you can return a value.

    You can then retrieve the value from the future returned by the executor:

    Future<Integer> future1 = executor.submit(ex1);
    Future<Integer> future2 = executor.submit(ex2);
    
    count1 = future1.get();
    count2 = future2.get();
    

    ps: you need to add error handling code around the future.get() calls

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in Java: import java.util.*; public class longest{ public static void
From the following code : import java.io.*; class fileTester { public static void main(
I have the following code: import java.net.InetAddress; public class lookup { public static void
I have the following simple code: import java.io.*; class IO { public static void
I have following code: import java.io.*; import java.util.concurrent.* ; public class Example{ public static
Say suppose I have the following Java code. public class Example { public static
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have the following code: import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; public final class

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.