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 or
013 * 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 data
015 * contained in the files.
016 *
017 * @param files
018 * @return
019 * @throws IOException
020 */
021 public static String[] threadedSort(File[] files) throws IOException
022 {
023 String[] sortedData = new String[0];
024 SortThread[] sortThreads = new SortThread[files.length];
025 for (int i=0; i
026 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 @Override
052 public void run() {
053 try {
054 data = Sort.getData(file);
055 for (int i = 0; i
056 //Thread.sleep(100);
057 //I commented this out because it was wasting time
058 //System.out.println(data[i]);
059 //I commented this out because I didn’t want to display the entire files
060 }
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 sorted
070 * list of the String data contained in each of the files.
071 *
072 * @param files the files to be read
073 * @return the sorted data
074 * @throws IOException thrown if any errors occur reading the file
075 */
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 specified
091 * file and return the data as an array of String objects.
092 *
093 * @param file the file containing the String data
094 * @return String array containing the String data
095 * @throws IOException thrown if any errors occur reading the file
096 */
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 reached
102 while (true) {
103 String line = in.readLine();
104 if (line == null) {
105 // the end of file was reached
106 break;
107 }
108 else {
109 data.add(line);
110 }
111 }
112 //Close the input stream and return the data
113 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 the
04 // 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 sorted
09 * @return the String objects sorted in ascending order
10 */
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 assumed
33 * to be sorted in ascending order. The method will return a
34 * sorted array of String objects containing all String objects
35 * from the two input collections.
36 *
37 * @param left a sorted collection of String objects
38 * @param right a sorted collection of String objects
39 * @return a sorted collection of String objects
40 */
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; i
49 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-threaded
06 * sort methods. This program will call each method to sort the data
07 * contained in the four test files. This program will then test the
08 * results to ensure that the results are sorted in ascending order.
09 *
10 * Simply run this program to verify that you have correctly implemented
11 * the threaded sort method. The program will not verify if your sort
12 * uses threads, but will verify if your implementation correctly
13 * 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 files
24 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 sorted
30 for (int i=0; i
31 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 sorted
40 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 sorted
46 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; i
51 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 Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.