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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:11:39+00:00 2026-06-10T06:11:39+00:00

The goal of this code is to run four threads, which will open four

  • 0

The goal of this code is to run four threads, which will open four text files, read the words from them and then place them into a string array,

Main problems i know of:

1- I am not putting the concurrent function in the void run function, I want to be able to pass parameters into that function

2- I am not sure if I am modifying the global arrays of strings or not either

First is the main method:

 public static void main(String[] args) throws IOException 
    {
         //declare the threads
         Thread thread1 =  new Thread(ReadFile("list1.txt", Global.array1,"thread1"));
         Thread thread2 =  new Thread(ReadFile("list2.txt", Global.array2,"thread2"));
         Thread thread3 =  new Thread(ReadFile("list3.txt", Global.array3,"thread1"));
         Thread thread4 =  new Thread(ReadFile("list4.txt", Global.array4,"thread2"));

         /*error message from netbeans: cannot find symbol
            symbol:   method ReadFile(java.lang.String,java.lang.String[])

            it says it for every delcaration of the thread*/


         thread1.start();  //putting the threads to work
         thread2.start();
         thread3.start();
         thread4.start();

         thread1.join();   //telling the threads to finish their work
         thread2.join();
         thread3.join();
         thread4.join();


         // merging the arrays into one
         List list = new ArrayList(Arrays.asList(Global.array1));
         list.addAll(Arrays.asList(Global.array2));
         list.addAll(Arrays.asList(Global.array3));
         list.addAll(Arrays.asList(Global.array4));
         Object[] theArray = list.toArray();

           -------------------------etc----------------------------

This is the “thread class” if my vocab is right

public class ReadFile implements Runnable
{
    public void run(){
            //I should get stuff here, that's my problem!!!!
    }


    private String path;
    Thread runner;

    public ReadFile(String filePath, String[] toArray, String threadName) throws IOException
    {
        String path = filePath;
        FileReader fr = new FileReader(path);
        BufferedReader textReader = new BufferedReader(fr);

        int numOfLines = readLines();  
        toArray = new String[numOfLines];

        int i;
        for (i=0; i<numOfLines; i++)
        {
            toArray[i]= textReader.readLine();    //place next line into string array
        }

        textReader.close();
    }


   int readLines() throws IOException
    {
        FileReader fr = new FileReader(filePath);
        BufferedReader bf = new BufferedReader(fr);

        String aLine;
        int noOfLines = 0;

        while((aLine = bf.readLine()) != null)
        {
            noOfLines++;
        }
        bf.close();
        return noOfLines;
    }

    }

Finally i did a class for the global variable, and i don’t know if it’s a good idea

public class Global 
{
    public static String[] array1;
    public static String[] array2;
    public static String[] array3;
    public static String[] array4;
}

please let me know what you guys think, any help or explanations or tips would be greatly appreciated

  • 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-10T06:11:40+00:00Added an answer on June 10, 2026 at 6:11 am

    If we first fix your file reading thread, so that the main work occurs in the run() method:

    public class FileReader extends Thread {
    
      private final File file;
      private String[] lines;
    
      public FileReader(File file) {
        this.file = file;
      }
    
      @Override
      public void run() {
        // Read file here (populate `lines`)..
      }
    
      public String[] getLines() {
        return lines;
      }
    }
    

    Then we can utilise this in the main method, as follows:

    public static void main(String[] args) throws Exception {
      List<FileReader> threads = new ArrayList<FileReader>();
    
      threads.add(new FileReader(new File("foo1")));
      threads.add(new FileReader(new File("foo2")));
      threads.add(new FileReader(new File("foo3")));
      threads.add(new FileReader(new File("foo4")));
    
      for (FileReader t : threads) {
        t.start();
      }
    
      List<String> allLines = new ArrayList<String>();
    
      for (FileReader t : threads) {
        t.join();
        allLines.addAll(Arrays.asList(t.getLines()));
      }    
    
      // File lines now in allLines
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are some questions on Code Metrics here, especially this one on goal values.
My goal is to get a Time instance from a DateTime instance This has
This is my code I have developed. This is the main program which holds
My main goal with this question is optimization and faster run time. After doing
I'm having trouble getting Code Analysis to run on the build server. My goal
After reading about semaphores I tried this test code in which I create two
Can anyone help me convert this code to make it run recursively? I'm not
I'm a beginner to C++ and I'm having a problem with this code, which
The end goal of this part of my project is to be able to
This is the goal: to remove Navigation Bar in some of the editors (no

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.