Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify this program so that it processes each file in a separate thread. This pr

ID: 3802746 • Letter: M

Question

Modify this program so that it processes each file in a separate thread. This program counts the amount of characters in each text file. The file names being processed are named “array.txt” “queue.txt” and “stack.txt"

This should speed up the program considerably, as the results below show.

--------------------------------------------------------------

The Program

public class threadActivity{

public static void main(String[] args) {
System.out.println("Single thread");
  
long startTime = System.currentTimeMillis();
  
File currentDirectory = new File(".");
  
for (File f: currentDirectory.listFiles()) { // gets all of the files in the current directory

if (!f.getName().endsWith(".txt")) {
continue; // skip non-text files
}
  
processFile(f);
}
  
long endTime = System.currentTimeMillis();
System.out.println("Processing took " + (endTime - startTime) / 1000 + " seconds");
  
}

  
public static void processFile(File f) {
int count = 0;
  
try {
Scanner in = new Scanner(f);
  
while (in.hasNext()) {
count += in.nextLine().toCharArray().length;
}
  
System.out.println(f.getName() + ": " + count);
  
} catch (Exception e) {
System.out.println("Trouble reading " + f.getAbsolutePath());
e.printStackTrace();
}
}
}

----------------------------------------------

Example Output

Single thread

array.txt: 230522880

queue.txt: 253755392

stack.txt: 210698240

Processing took 25 seconds

Multi-threaded

array.txt: 230522880

stack.txt: 210698240

queue.txt:

253755392

Processing took 16 seconds

Explanation / Answer

import java.io.File;
import java.util.Scanner;

public class threadActivity {
   public static void main(String[] args) {

       long startTime = System.currentTimeMillis();

       File currentDirectory = new File(".");

       for (File f : currentDirectory.listFiles()) { // gets all of the files
                                                       // in the current
                                                       // directory
           if (!f.getName().endsWith(".txt")) {
               continue; // skip non-text files
           }

           Thread t=new Thread() {
               public void run() {
                   System.out.println("Thread Is :"+ getName());
                   int count = 0;

                   try {
                       Scanner in = new Scanner(f);

                       while (in.hasNext()) {
                           count += in.nextLine().toCharArray().length;
                       }

                       System.out.println(f.getName() + ": " + count);

                   } catch (Exception e) {
                       System.out.println("Trouble reading " + f.getAbsolutePath());
                       e.printStackTrace();
                   }
               }
           };
           t.start();
       }
       long endTime = System.currentTimeMillis();
       System.out.println("Processing took " + (endTime - startTime)/1000 + " seconds");

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote