Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago