Question: Question is below along with my code. Can you see why my non-threaded is faster than my threaded code....code is below with results from NetBeans.

Question is below along with my code. Can you see why my non-threaded is faster than my threaded code....code is below with results from NetBeans. Thought the non-threaded was slower than the threaded.

Modify the Java program by adding threads, and analyze the performance of both the threaded and non-threaded versions. Modify the Program: 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. Analyze the Program: When running the provided SortTest program, the output presents data to support analyzing the performance of the threaded and non-threaded sort methods. Analyze your threaded implementation by comparing its performance to the original non-threaded implementation and explain the measured behavior. Document your analysis as a short paper (13 pages), using APA format. Be sure to discuss the relative performance improvement you expect for your threaded implementation and how the expected performance compares to the measured performance.

NetBean Results

run: The data returned by Sort.sort is sorted. Sort.sort took 0.449187919 seconds to read and sort the data. The data returned by Sort.threadedSort is sorted. Sort.threadedSort took 1.321305193 seconds to read and sort the data. BUILD SUCCESSFUL (total time: 1 second)

***MegaSort***

public class MergeSort { // The mergeSort method returns a sorted copy of the // String objects contained in the String array data. /** * Sorts the String objects using the merge sort algorithm. * * @param data the String objects to be sorted * @return the String objects sorted in ascending order */ public static String[] mergeSort(String[] data) {

if (data.length > 1) { String[] left = new String[data.length / 2]; String[] right = new String[data.length - left.length]; System.arraycopy(data, 0, left, 0, left.length); System.arraycopy(data, left.length, right, 0, right.length); left = mergeSort(left); right = mergeSort(right); return merge(left, right); } else { return data; } } /** * The merge method accepts two String arrays that are assumed * to be sorted in ascending order. The method will return a * sorted array of String objects containing all String objects * from the two input collections. * * @param left a sorted collection of String objects * @param right a sorted collection of String objects * @return a sorted collection of String objects */ public static String[] merge(String[] left, String[] right) { String[] data = new String[left.length + right.length]; int lIndex = 0; int rIndex = 0; for (int i=0; i

***Sort****

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

public class Sort {

/** * You are to implement this method. The method should invoke one or * more threads to read and sort the data from the collection of Files. * The method should return a sorted list of all of the String data * contained in the files. * * @param files * @return * @throws IOException */ public static String[] threadedSort(File[] files) throws IOException { String sortedData[] = new String[0]; // To be returned from this method int counter = 0; boolean allThreadsTerminated = false; // Create array of SortingThread with length same as number of files SortingThread[] threadList = new SortingThread[files.length]; // Instantiate each thread and pass the Data to it, start each of them in parallel for (File file : files) { String[] data = getData(file); threadList[counter] = new SortingThread(data); threadList[counter].start(); counter++; } // Wait until all threads are not terminated. // You must keep the data preserved until all threads are completed. while(!allThreadsTerminated) { allThreadsTerminated = true; for(counter=0; counter

String[] sortedData = new String[0];

for (File file : files) { String[] data = getData(file); data = MergeSort.mergeSort(data); sortedData = MergeSort.merge(sortedData, data); }

return sortedData;

}

/** * This method will read in the string data from the specified * file and return the data as an array of String objects. * * @param file the file containing the String data * @return String array containing the String data * @throws IOException thrown if any errors occur reading the file */ private static String[] getData(File file) throws IOException {

ArrayList data = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader(file));

// Read the data from the file until the end of file is reached while (true) { String line = in.readLine(); if (line == null) { // the end of file was reached break; } else { data.add(line); } } //Close the input stream and return the data in.close(); return data.toArray(new String[0]);

} }

***SortTest***

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

public class Sort {

/** * You are to implement this method. The method should invoke one or * more threads to read and sort the data from the collection of Files. * The method should return a sorted list of all of the String data * contained in the files. * * @param files * @return * @throws IOException */ public static String[] threadedSort(File[] files) throws IOException { String sortedData[] = new String[0]; // To be returned from this method int counter = 0; boolean allThreadsTerminated = false; // Create array of SortingThread with length same as number of files SortingThread[] threadList = new SortingThread[files.length]; // Instantiate each thread and pass the Data to it, start each of them in parallel for (File file : files) { String[] data = getData(file); threadList[counter] = new SortingThread(data); threadList[counter].start(); counter++; } // Wait until all threads are not terminated. // You must keep the data preserved until all threads are completed. while(!allThreadsTerminated) { allThreadsTerminated = true; for(counter=0; counter

String[] sortedData = new String[0];

for (File file : files) { String[] data = getData(file); data = MergeSort.mergeSort(data); sortedData = MergeSort.merge(sortedData, data); }

return sortedData;

}

/** * This method will read in the string data from the specified * file and return the data as an array of String objects. * * @param file the file containing the String data * @return String array containing the String data * @throws IOException thrown if any errors occur reading the file */ private static String[] getData(File file) throws IOException {

ArrayList data = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader(file));

// Read the data from the file until the end of file is reached while (true) { String line = in.readLine(); if (line == null) { // the end of file was reached break; } else { data.add(line); } } //Close the input stream and return the data in.close(); return data.toArray(new String[0]);

} }

***SortingThread***

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

public class Sort {

/** * You are to implement this method. The method should invoke one or * more threads to read and sort the data from the collection of Files. * The method should return a sorted list of all of the String data * contained in the files. * * @param files * @return * @throws IOException */ public static String[] threadedSort(File[] files) throws IOException { String sortedData[] = new String[0]; // To be returned from this method int counter = 0; boolean allThreadsTerminated = false; // Create array of SortingThread with length same as number of files SortingThread[] threadList = new SortingThread[files.length]; // Instantiate each thread and pass the Data to it, start each of them in parallel for (File file : files) { String[] data = getData(file); threadList[counter] = new SortingThread(data); threadList[counter].start(); counter++; } // Wait until all threads are not terminated. // You must keep the data preserved until all threads are completed. while(!allThreadsTerminated) { allThreadsTerminated = true; for(counter=0; counter

String[] sortedData = new String[0];

for (File file : files) { String[] data = getData(file); data = MergeSort.mergeSort(data); sortedData = MergeSort.merge(sortedData, data); }

return sortedData;

}

/** * This method will read in the string data from the specified * file and return the data as an array of String objects. * * @param file the file containing the String data * @return String array containing the String data * @throws IOException thrown if any errors occur reading the file */ private static String[] getData(File file) throws IOException {

ArrayList data = new ArrayList(); BufferedReader in = new BufferedReader(new FileReader(file));

// Read the data from the file until the end of file is reached while (true) { String line = in.readLine(); if (line == null) { // the end of file was reached break; } else { data.add(line); } } //Close the input stream and return the data in.close(); return data.toArray(new String[0]);

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!