Question: //== Sort.java === import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Sort { private static ExecutorService executorService
//== Sort.java ===
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Sort {
private static ExecutorService executorService = Executors
.newFixedThreadPool(2);
/**
* 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 {
for (final File file : files) {
executorService.execute(new SortThread(file));
}
executorService.shutdown();
while(!executorService.isTerminated()){
// do nothing wait for termination
}
return SortThread.sortedData;
}
/**
* Given an array of files, this method will return a sorted list of the
* String data contained in each of the 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
*/
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]);
}
}
//==
//==-----SortThread ==
import java.io.File;
import java.io.IOException;
public class SortThread implements Runnable {
private File file;
static String[] sortedData=new String[0];;
public SortThread(File file) {
super();
this.file = file;
}
@Override
public void run() {
String[] data = new String[0];
try {
data = Sort.getData(file);
} catch (IOException e) {
e.printStackTrace();
}
data = MergeSort.mergeSort(data);
synchronized (sortedData) {
sortedData = MergeSort.merge(sortedData, data);
}
}
}
//==
The assignemt is below.
Answer the questions below about the above netbean process
Evaluate the threaded implementation of the sort algorithm relative to data integrity and concurrency control.
By Day 7, write a 2- to 3-page paper evaluating the implementation of the sort algorithm. Answer the following questions:
Is the threaded implementation correct, or are there data integrity concerns due to concurrency control?
If your implementation is correct, what protections are in place to ensure atomicity and consistency? Could a more effective method of ensuring data integrity be implemented in your solution?
If you ran into trouble with your implementation, provide specifics about the data integrity problems you encountered and explain the modifications that will be necessary to correct the data integrity and concurrency issues.
Include in your analysis an explanation of the operation of the threaded implementation, and if there are data integrity issues, suggest one or more modifications to the implementation to resolve these concerns.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
