Improve the performance of the Java program by adding threads to the Sort.java file. Implement the threadedSort() method within the Sort class. (Reuse any of the existing methods by calling them as necessary from your threadedSort method. You may add additional methods to the Sort class, if necessary.)Document your analysis as a short paper (1 page).Main File (sort.java):001import java.io.BufferedReader;002import java.io.File;003import java.io.FileReader;004import java.io.IOException;005import java.util.ArrayList;006import java.util.logging.Level;007import java.util.logging.Logger;008 009public class Sort {010 011 /**012 * You are to implement this method. The method should invoke one or013 * more threads to read and sort the data from the collection of Files.014 * The method should return a sorted list of all of the String data015 * contained in the files.016 *017 * @param files018 * @return019 * @throws IOException020 */021 public static String[] threadedSort(File[] files) throws IOException022 {023 String[] sortedData = new String[0];024 SortThread[] sortThreads = new SortThread[files.length];025 for (int i=0; i026 sortThreads[i] = new SortThread(files[i]);027 sortThreads[i].start();028 }029 030 for (SortThread thread : sortThreads) {031 try {032 thread.join();033 }034 catch (InterruptedException ex){035 Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);036 }037 return sortedData;038 }039 throw new java.lang.IllegalStateException(“Method not implemented”);040 }041 042 private static class SortThread extends Thread {043 private File file;044 private String[] data;045 046 047 public SortThread(File file) {048 this.file = file;049 }050 051 @Override052 public void run() {053 try {054 data = Sort.getData(file);055 for (int i = 0; i056 //Thread.sleep(100); 057 //I commented this out because it was wasting time058 //System.out.println(data[i]);059 //I commented this out because I didn’t want to display the entire files060 }061 }062 catch (IOException ex) {063 Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);064 }065 }066 }067 068 /**069 * Given an array of files, this method will return a sorted070 * list of the String data contained in each of the files.071 *072 * @param files the files to be read073 * @return the sorted data074 * @throws IOException thrown if any errors occur reading the file075 */076 public static String[] sort(File[] files) throws IOException {077 078 String[] sortedData = new String[0];079 080 for (File file : files) {081 String[] data = getData(file);082 data = MergeSort.mergeSort(data);083 sortedData = MergeSort.merge(sortedData, data);084 }085 086 return sortedData;087 088 }089 /**090 * This method will read in the string data from the specified091 * file and return the data as an array of String objects.092 *093 * @param file the file containing the String data094 * @return String array containing the String data095 * @throws IOException thrown if any errors occur reading the file096 */097 private static String[] getData(File file) throws IOException {098 099 ArrayList data = new ArrayList();100 BufferedReader in = new BufferedReader(new FileReader(file));101 // Read the data from the file until the end of file is reached102 while (true) {103 String line = in.readLine();104 if (line == null) {105 // the end of file was reached106 break;107 }108 else {109 data.add(line);110 }111 }112 //Close the input stream and return the data113 in.close();114 return data.toArray(new String[0]);115 116 }117}Mergesort.java:01public class MergeSort {02 03 // The mergeSort method returns a sorted copy of the04 // String objects contained in the String array data.05 /**06 * Sorts the String objects using the merge sort algorithm.07 *08 * @param data the String objects to be sorted09 * @return the String objects sorted in ascending order10 */11 public static String[] mergeSort(String[] data) {12 13 if (data.length > 1) {14 String[] left = new String[data.length / 2];15 String[] right = new String[data.length – left.length];16 System.arraycopy(data, 0, left, 0, left.length);17 System.arraycopy(data, left.length, right, 0, right.length);18 19 left = mergeSort(left);20 right = mergeSort(right);21 22 return merge(left, right);23 24 }25 else {26 return data;27 }28 29 }30 31 /**32 * The merge method accepts two String arrays that are assumed33 * to be sorted in ascending order. The method will return a34 * sorted array of String objects containing all String objects35 * from the two input collections.36 *37 * @param left a sorted collection of String objects38 * @param right a sorted collection of String objects39 * @return a sorted collection of String objects40 */41 public static String[] merge(String[] left, String[] right) {42 43 String[] data = new String[left.length + right.length];44 45 int lIndex = 0;46 int rIndex = 0;47 48 for (int i=0; i49 if (lIndex == left.length) {50 data[i] = right[rIndex];51 rIndex++;52 }53 else if (rIndex == right.length) {54 data[i] = left[lIndex];55 lIndex++;56 }57 else if (left[lIndex].compareTo(right[rIndex]) < 0) {58 data[i] = left[lIndex];59 lIndex++;60 }61 else {62 data[i] = right[rIndex];63 rIndex++;64 }65 }66 67 return data;68 69 }70 71}SortTest.java:01import java.io.File;02import java.io.IOException;03 04/**05 * The class SortTest is used to test the threaded and non-threaded06 * sort methods. This program will call each method to sort the data07 * contained in the four test files. This program will then test the08 * results to ensure that the results are sorted in ascending order.09 *10 * Simply run this program to verify that you have correctly implemented11 * the threaded sort method. The program will not verify if your sort12 * uses threads, but will verify if your implementation correctly13 * sorted the data contained in the four files.14 *15 * There should be no reason to make modifications to this class.16 */17public class SortTest {18 19 public static void main(String[] args) throws IOException {20 21 File[] files = {new File("enable1.txt"), new File("enable2k.txt"), newFile("lower.txt"), new File("mixed.txt")};22 23 // Run Sort.sort on the files24 long startTime = System.nanoTime();25 String[] sortedData = Sort.sort(files);26 long stopTime = System.nanoTime();27 double elapsedTime = (stopTime - startTime) / 1000000000.0;28 29 // Test to ensure the data is sorted30 for (int i=0; i31 if (sortedData[i].compareTo(sortedData[i+1]) > 0) {32 System.out.println(“The data returned by Sort.sort is not sorted.”);33 throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);34 }35 }36 System.out.println(“The data returned by Sort.sort is sorted.”);37 System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);38 39 // Run Sort.threadedSort on the files and test to ensure the data is sorted40 startTime = System.nanoTime();41 String[] threadSortedData = Sort.threadedSort(files);42 stopTime = System.nanoTime();43 double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;44 45 // Test to ensure the data is sorted46 if (sortedData.length != threadSortedData.length) {47 System.out.println(“The data return by Sort.threadedSort is missing data”);48 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);49 }50 for (int i=0; i51 if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {52 System.out.println(“The data return by Sort.threadedSort is not sorted”);53 throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);54 }55 }56 System.out.println(“The data returned by Sort.threadedSort is sorted.”);57 System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);58 59 60 }61 62}The error am getting:run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)
Why Choose Us
- 100% non-plagiarized Papers
- 24/7 /365 Service Available
- Affordable Prices
- Any Paper, Urgency, and Subject
- Will complete your papers in 6 hours
- On-time Delivery
- Money-back and Privacy guarantees
- Unlimited Amendments upon request
- Satisfaction guarantee
How it Works
- Click on the “Place Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
- Fill in your paper’s requirements in the "PAPER DETAILS" section.
- Fill in your paper’s academic level, deadline, and the required number of pages from the drop-down menus.
- Click “CREATE ACCOUNT & SIGN IN” to enter your registration details and get an account with us for record-keeping and then, click on “PROCEED TO CHECKOUT” at the bottom of the page.
- From there, the payment sections will show, follow the guided payment process and your order will be available for our writing team to work on it.