Question
In previous assignments, you used threads to improve the performance of a sort algorithm. Threads were initially applied in order to achieve processing concurrency in
In previous assignments, you used threads to improve the performance of a sort algorithm. Threads were initially applied in order to achieve processing concurrency in order to reduce the time required to sort data. This weeks readings highlighted various ways that I/O performance can impact the overall performance of an application or a system. The readings also identified specific principles that can be applied to improve the efficiency of I/O.
For this Assignment, you will consider the I/O performed in the threaded sort Assignment from Week 2 and how the I/O contributes to the performance of the threaded sort. Applying the principles identified in this weeks reading, along with the concurrency control mechanisms from Week 3, you will revise the threaded sort application in an effort to improve the I/O performance in order to affect an overall performance improvement.
To prepare:
Evaluate the manner by which the threaded sort application performs I/O to retrieve the data to be sorted.
Propose a strategy to improve the performance by applying one or more of the principles to improve the efficiency of I/O that were identified in this weeks reading.
By Day 7, implement your strategy by modifying your solution to the threaded sort Assignment from Week 2.
In addition, write a 2- to 3-page paper that evaluates how I/O performance impacts overall program performance. Make sure to include the following:
A description of your I/O strategy
An explanation on how you expected your strategy to improve performance
A summary of the actual change in performance observed when running the updated threaded sort
Threaded Sort
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Sort implements Runnable {
private static Object lock;
private static String[] sortedData;
File file; // Each thread is responsible for sorting one file
public Sort(File file) {
this.file = file;
}
/**
* 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 {
lock = new Object();
sortedData = new String[0];
Thread[] threads = new Thread[files.length];
for (int i = 0; i < files.length; i++) {
threads[i] = new Thread(new Sort(files[i]));
threads[i].start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return sortedData;
}
@Override
public void run() {
String[] data;
try {
data = getData(file);
data = MergeSort.mergeSort(data);
synchronized(lock) {
sortedData = MergeSort.merge(sortedData, data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Given an array of files, this method will return a sorted list of the
* String data contained in each of the files.
*
* @param files
*
* @param files
* the files to be read
* @return the sorted data
* @throws IOException
* thrown if any errors occur reading the file
*/
public static String[] sort(File[] files) throws IOException {
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started