Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a 2- to 3-page paper evaluating the implementation of the sort algorithm.

ID: 3736822 • Letter: W

Question

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<String> data = new ArrayList<String>();

                                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]);

                }

}

Explanation / Answer

Is the threaded implementation correct, or are there data integrity concerns due to concurrency control?

Yes the threaded implementation is correct , and both versions of threaded and unthreaded sort methods are given where we can see in unthreaded version while merging finally all the files the data in one file should be compared and merged with another, so there comes the conflict of sharing a data and concurrency problem arises.

In threaded version they are implemented in Threads and synchronized by synchronized block.

If your implementation is correct, what protections are in place to ensure atomicity and consistency?

Each thread is assigned a separate file and all are joined at the end and all are protected by using synchronized block.

Could a more effective method of ensuring data integrity be implemented in your solution?

Entire code be synchronized and enclosed in try-catch blocks will ensure robustness.

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.

Merge files in places of half ,half/2… and so on to reduce complexity also data integrity and memory issues may not arise. Ensure that each half of file is synchronized with mergesort process.