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

need help with this lab please and thank you :D Directions You will need to down

ID: 3802406 • Letter: N

Question

need help with this lab please and thank you :D

Directions

You will need to download the file from https://tinyurl.com/n7rdej8 (when you put this in browser a download will start, that what is needed to do this)

This file is somewhat large because it has several big text files.

The code in the zip file counts the number of characters in all of the text files in the directory from which the program is run.

This may take awhile to run (on my computer it took about 25 seconds).

Your task for this lab is to modify this program so that it processes each file in a separate thread. This should speed up the program considerably, as the results below show. IMPORTANT: Your program still needs to accurately measure the time it takes to process all of the files.

One way to do this is to create an ArrayList of Threads at the beginning of main. As you create each thread, add it to the ArrayList before you start it. Then, before you get the end time, call the join method on each Thread in the ArrayList.

Example **** SAMPLE 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

here is what the starting code looks like***

package lab.pkg8a;

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


public class Lab8a {

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

Explanation / Answer

package lab.pkg8a;

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


public class Lab8a {

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