Improve This Paper

Improve this paper.

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

Improve The Performance Of The Java Program By Adding Threads To The Sort Java F 2

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

Document your analysis as a short paper (1 page).

Main File (sort.java):

001import java.io.BufferedReader;

002import java.io.File;

003import java.io.FileReader;

004import java.io.IOException;

005import java.util.ArrayList;

006import java.util.logging.Level;

007import java.util.logging.Logger;

008 

009public class Sort {

010 

011  /**

012   * You are to implement this method. The method should invoke one or

013   * more threads to read and sort the data from the collection of Files.

014   * The method should return a sorted list of all of the String data

015   * contained in the files.

016   *

017   * @param files

018   * @return

019   * @throws IOException

020   */

021  public static String[] threadedSort(File[] files) throws IOException

022  {

023      String[] sortedData = new String[0];

024      SortThread[] sortThreads = new SortThread[files.length];

025      for (int i=0; i

026          sortThreads[i] = new SortThread(files[i]);

027          sortThreads[i].start();

028      }

029       

030      for (SortThread thread : sortThreads) {

031          try {

032              thread.join();

033          }

034          catch (InterruptedException ex){

035              Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);

036          }

037          return sortedData;

038      }

039      throw new java.lang.IllegalStateException(“Method not implemented”);

040  }

041   

042  private static class SortThread extends Thread {

043      private File file;

044      private String[] data;

045 

046       

047      public SortThread(File file) {

048          this.file = file;

049      }

050       

051      @Override

052      public void run() {

053          try {

054              data = Sort.getData(file);

055              for (int i = 0; i

056                  //Thread.sleep(100);               

057                 //I commented this out because it was wasting time

058                  //System.out.println(data[i]);

059                  //I commented this out because I didn’t want to display the entire files

060              }

061          }

062          catch (IOException ex) {

063              Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);

064          }

065    }

066  }

067   

068  /**

069   * Given an array of files, this method will return a sorted

070   * list of the String data contained in each of the files.

071   *

072   * @param files the files to be read

073   * @return the sorted data

074   * @throws IOException thrown if any errors occur reading the file

075   */

076  public static String[] sort(File[] files) throws IOException {

077 

078    String[] sortedData = new String[0];

079 

080    for (File file : files) {

081      String[] data = getData(file);

082      data = MergeSort.mergeSort(data);

083      sortedData = MergeSort.merge(sortedData, data);

084    }

085 

086    return sortedData;

087 

088  }

089  /**

090   * This method will read in the string data from the specified

091   * file and return the data as an array of String objects.

092   *

093   * @param file the file containing the String data

094   * @return String array containing the String data

095   * @throws IOException thrown if any errors occur reading the file

096   */

097  private static String[] getData(File file) throws IOException {

098 

099    ArrayList data = new ArrayList();

100    BufferedReader in = new BufferedReader(new FileReader(file));

101    // Read the data from the file until the end of file is reached

102    while (true) {

103      String line = in.readLine();

104      if (line == null) {

105        // the end of file was reached

106        break;

107      }

108      else {

109        data.add(line);

110      }

111    }

112    //Close the input stream and return the data

113    in.close();

114    return data.toArray(new String[0]);

115 

116  }

117}

Mergesort.java:

01public class MergeSort {

02   

03  // The mergeSort method returns a sorted copy of the

04  // String objects contained in the String array data.

05  /**

06   * Sorts the String objects using the merge sort algorithm.

07   *

08   * @param data the String objects to be sorted

09   * @return the String objects sorted in ascending order

10   */

11  public static String[] mergeSort(String[] data) {

12 

13    if (data.length > 1) {

14      String[] left = new String[data.length / 2];

15      String[] right = new String[data.length – left.length];

16      System.arraycopy(data, 0, left, 0, left.length);

17      System.arraycopy(data, left.length, right, 0, right.length);

18     

19      left = mergeSort(left);

20      right = mergeSort(right);

21     

22      return merge(left, right);

23       

24    }

25    else {

26      return data;

27    }

28     

29  }

30   

31  /**

32   * The merge method accepts two String arrays that are assumed

33   * to be sorted in ascending order. The method will return a

34   * sorted array of String objects containing all String objects

35   * from the two input collections.

36   *

37   * @param left a sorted collection of String objects

38   * @param right a sorted collection of String objects

39   * @return a sorted collection of String objects

40   */

41  public static String[] merge(String[] left, String[] right) {

42     

43    String[] data = new String[left.length + right.length];

44     

45    int lIndex = 0;

46    int rIndex = 0;

47     

48    for (int i=0; i

49      if (lIndex == left.length) {

50        data[i] = right[rIndex];

51        rIndex++;

52      }

53      else if (rIndex == right.length) {

54        data[i] = left[lIndex];

55        lIndex++;

56      }

57      else if (left[lIndex].compareTo(right[rIndex]) < 0) {

58        data[i] = left[lIndex];

59        lIndex++;

60      }

61      else {

62        data[i] = right[rIndex];

63        rIndex++;

64      }

65    }

66     

67    return data;

68     

69  }

70   

71}

SortTest.java:

01import java.io.File;

02import java.io.IOException;

03 

04/**

05 * The class SortTest is used to test the threaded and non-threaded

06 * sort methods. This program will call each method to sort the data

07 * contained in the four test files. This program will then test the

08 * results to ensure that the results are sorted in ascending order.

09 *

10 * Simply run this program to verify that you have correctly implemented

11 * the threaded sort method. The program will not verify if your sort

12 * uses threads, but will verify if your implementation correctly

13 * sorted the data contained in the four files.

14 *

15 * There should be no reason to make modifications to this class.

16 */

17public class SortTest {

18   

19  public static void main(String[] args) throws IOException {

20     

21    File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), newFile(“lower.txt”), new File(“mixed.txt”)};

22 

23    // Run Sort.sort on the files

24    long startTime = System.nanoTime();

25    String[] sortedData = Sort.sort(files);

26    long stopTime = System.nanoTime();

27    double elapsedTime = (stopTime – startTime) / 1000000000.0;

28     

29    // Test to ensure the data is sorted

30    for (int i=0; i

31      if (sortedData[i].compareTo(sortedData[i+1]) > 0) {

32        System.out.println(“The data returned by Sort.sort is not sorted.”);

33        throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);

34      }

35    }

36    System.out.println(“The data returned by Sort.sort is sorted.”);

37    System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);

38     

39    // Run Sort.threadedSort on the files and test to ensure the data is sorted

40    startTime = System.nanoTime();

41    String[] threadSortedData = Sort.threadedSort(files);

42    stopTime = System.nanoTime();

43    double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;

44     

45    // Test to ensure the data is sorted

46    if (sortedData.length != threadSortedData.length) {

47      System.out.println(“The data return by Sort.threadedSort is missing data”);

48      throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);

49    }

50    for (int i=0; i

51      if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {

52        System.out.println(“The data return by Sort.threadedSort is not sorted”);

53        throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);

54      }

55    }

56    System.out.println(“The data returned by Sort.threadedSort is sorted.”);

57    System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);

58     

59     

60  }

61   

62}

The error am getting:

run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

Improve The Performance Of The Java Program By Adding Threads To The Sort Java F 1

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

Document your analysis as a short paper (1 page).

Main File (sort.java):

001import java.io.BufferedReader;

002import java.io.File;

003import java.io.FileReader;

004import java.io.IOException;

005import java.util.ArrayList;

006import java.util.logging.Level;

007import java.util.logging.Logger;

008 

009public class Sort {

010 

011  /**

012   * You are to implement this method. The method should invoke one or

013   * more threads to read and sort the data from the collection of Files.

014   * The method should return a sorted list of all of the String data

015   * contained in the files.

016   *

017   * @param files

018   * @return

019   * @throws IOException

020   */

021  public static String[] threadedSort(File[] files) throws IOException

022  {

023      String[] sortedData = new String[0];

024      SortThread[] sortThreads = new SortThread[files.length];

025      for (int i=0; i

026          sortThreads[i] = new SortThread(files[i]);

027          sortThreads[i].start();

028      }

029       

030      for (SortThread thread : sortThreads) {

031          try {

032              thread.join();

033          }

034          catch (InterruptedException ex){

035              Logger.getLogger(Sort.class.getName()).log(Level.SEVERE, null, ex);

036          }

037          return sortedData;

038      }

039      throw new java.lang.IllegalStateException(“Method not implemented”);

040  }

041   

042  private static class SortThread extends Thread {

043      private File file;

044      private String[] data;

045 

046       

047      public SortThread(File file) {

048          this.file = file;

049      }

050       

051      @Override

052      public void run() {

053          try {

054              data = Sort.getData(file);

055              for (int i = 0; i

056                  //Thread.sleep(100);               

057                 //I commented this out because it was wasting time

058                  //System.out.println(data[i]);

059                  //I commented this out because I didn’t want to display the entire files

060              }

061          }

062          catch (IOException ex) {

063              Logger.getLogger (Sort.class.getName()).log(Level.SEVERE, null, ex);

064          }

065    }

066  }

067   

068  /**

069   * Given an array of files, this method will return a sorted

070   * list of the String data contained in each of the files.

071   *

072   * @param files the files to be read

073   * @return the sorted data

074   * @throws IOException thrown if any errors occur reading the file

075   */

076  public static String[] sort(File[] files) throws IOException {

077 

078    String[] sortedData = new String[0];

079 

080    for (File file : files) {

081      String[] data = getData(file);

082      data = MergeSort.mergeSort(data);

083      sortedData = MergeSort.merge(sortedData, data);

084    }

085 

086    return sortedData;

087 

088  }

089  /**

090   * This method will read in the string data from the specified

091   * file and return the data as an array of String objects.

092   *

093   * @param file the file containing the String data

094   * @return String array containing the String data

095   * @throws IOException thrown if any errors occur reading the file

096   */

097  private static String[] getData(File file) throws IOException {

098 

099    ArrayList data = new ArrayList();

100    BufferedReader in = new BufferedReader(new FileReader(file));

101    // Read the data from the file until the end of file is reached

102    while (true) {

103      String line = in.readLine();

104      if (line == null) {

105        // the end of file was reached

106        break;

107      }

108      else {

109        data.add(line);

110      }

111    }

112    //Close the input stream and return the data

113    in.close();

114    return data.toArray(new String[0]);

115 

116  }

117}

Mergesort.java:

01public class MergeSort {

02   

03  // The mergeSort method returns a sorted copy of the

04  // String objects contained in the String array data.

05  /**

06   * Sorts the String objects using the merge sort algorithm.

07   *

08   * @param data the String objects to be sorted

09   * @return the String objects sorted in ascending order

10   */

11  public static String[] mergeSort(String[] data) {

12 

13    if (data.length > 1) {

14      String[] left = new String[data.length / 2];

15      String[] right = new String[data.length – left.length];

16      System.arraycopy(data, 0, left, 0, left.length);

17      System.arraycopy(data, left.length, right, 0, right.length);

18     

19      left = mergeSort(left);

20      right = mergeSort(right);

21     

22      return merge(left, right);

23       

24    }

25    else {

26      return data;

27    }

28     

29  }

30   

31  /**

32   * The merge method accepts two String arrays that are assumed

33   * to be sorted in ascending order. The method will return a

34   * sorted array of String objects containing all String objects

35   * from the two input collections.

36   *

37   * @param left a sorted collection of String objects

38   * @param right a sorted collection of String objects

39   * @return a sorted collection of String objects

40   */

41  public static String[] merge(String[] left, String[] right) {

42     

43    String[] data = new String[left.length + right.length];

44     

45    int lIndex = 0;

46    int rIndex = 0;

47     

48    for (int i=0; i

49      if (lIndex == left.length) {

50        data[i] = right[rIndex];

51        rIndex++;

52      }

53      else if (rIndex == right.length) {

54        data[i] = left[lIndex];

55        lIndex++;

56      }

57      else if (left[lIndex].compareTo(right[rIndex]) < 0) {

58        data[i] = left[lIndex];

59        lIndex++;

60      }

61      else {

62        data[i] = right[rIndex];

63        rIndex++;

64      }

65    }

66     

67    return data;

68     

69  }

70   

71}

SortTest.java:

01import java.io.File;

02import java.io.IOException;

03 

04/**

05 * The class SortTest is used to test the threaded and non-threaded

06 * sort methods. This program will call each method to sort the data

07 * contained in the four test files. This program will then test the

08 * results to ensure that the results are sorted in ascending order.

09 *

10 * Simply run this program to verify that you have correctly implemented

11 * the threaded sort method. The program will not verify if your sort

12 * uses threads, but will verify if your implementation correctly

13 * sorted the data contained in the four files.

14 *

15 * There should be no reason to make modifications to this class.

16 */

17public class SortTest {

18   

19  public static void main(String[] args) throws IOException {

20     

21    File[] files = {new File(“enable1.txt”), new File(“enable2k.txt”), newFile(“lower.txt”), new File(“mixed.txt”)};

22 

23    // Run Sort.sort on the files

24    long startTime = System.nanoTime();

25    String[] sortedData = Sort.sort(files);

26    long stopTime = System.nanoTime();

27    double elapsedTime = (stopTime – startTime) / 1000000000.0;

28     

29    // Test to ensure the data is sorted

30    for (int i=0; i

31      if (sortedData[i].compareTo(sortedData[i+1]) > 0) {

32        System.out.println(“The data returned by Sort.sort is not sorted.”);

33        throw new java.lang.IllegalStateException(“The data returned by Sort.sort is not sorted”);

34      }

35    }

36    System.out.println(“The data returned by Sort.sort is sorted.”);

37    System.out.println(“Sort.sort took ” + elapsedTime + ” seconds to read and sort the data.”);

38     

39    // Run Sort.threadedSort on the files and test to ensure the data is sorted

40    startTime = System.nanoTime();

41    String[] threadSortedData = Sort.threadedSort(files);

42    stopTime = System.nanoTime();

43    double threadedElapsedTime = (stopTime – startTime)/ 1000000000.0;

44     

45    // Test to ensure the data is sorted

46    if (sortedData.length != threadSortedData.length) {

47      System.out.println(“The data return by Sort.threadedSort is missing data”);

48      throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);

49    }

50    for (int i=0; i

51      if (threadSortedData[i].compareTo(threadSortedData[i+1]) > 0) {

52        System.out.println(“The data return by Sort.threadedSort is not sorted”);

53        throw new java.lang.IllegalStateException(“The data returned by Sort.threadedSort is not sorted”);

54      }

55    }

56    System.out.println(“The data returned by Sort.threadedSort is sorted.”);

57    System.out.println(“Sort.threadedSort took ” + threadedElapsedTime + ” seconds to read and sort the data.”);

58     

59     

60  }

61   

62}

The error am getting:

run:The data returned by Sort.sort is sorted.Sort.sort took 2.072120353 seconds to read and sort the data.The data return by Sort.threadedSort is missing dataException in thread “main” java.lang.IllegalStateException: The data returned by Sort.threadedSort is not sortedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (total time: 3 seconds)

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

In 100 To 150 Words Develop A Logical Argument About Why The Jews Maintained The 1

In 100 to 150 words, develop a logical argument about why the Jews maintained their national identity despite three major attempts at their destruction.

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

Improve The Paper The Following Two Attachments One Is The Requirements Of The P

Homework cancelled

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

In 1 To 2 Page Paper Addresses The Following Describe A Child And Adolescent Gro

In 1 to 2 page paper addresses the following:

Describe a child and adolescent group you are counseling.

Describe a client from the group who you do not think is adequately progressing according to expected clinical outcomes. Note: Do not use the client’s actual name.

Explain your therapeutic approach with the group, including your perceived effectiveness of your approach with the client you identified.

Identify any additional information about this group and/or client that may potentially impact expected outcomes.

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

Improve The Following Business Message Our E Tailing Site Www Bestbabygear Com S

Improve the following business message: Our e-tailing site, www.BestBabyGear.com, specializes in only the very best products for parents of newborns, infants, and toddlers. We constantly scour the world looking for products that are good enough and well-built enough and classy enough good enough that is to take their place alongside the hundreds of other carefully selected products that adorn the pages of our award-winning website, www.bestbabygear.com. We aim for the fences every time we select a product to join this portfolio; we don t want to waste our time with onesey-twosey products that might sell a half dozen units per annum no, we want every product to be a top-drawer success, selling at least one hundred units per specific model per year in order to justify our expense and hassle factor in adding it to the abovementioned portfolio. After careful consideration, we thusly concluded that your Inglesina lines meet our needs and would therefore like to add it.

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

In 1 Page Read Http Www Nytimes Com 2015 05 23 Us Politics Obama Set To Strength

in 1 page . read

summarize the article . 

Obama Plans New Rule to Limit Water Pollutionr there  good solutions did  they make ? do u agree with solutions ?

are there any improvements ? and would u suggest or add  soutloutins? 

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

Imprints 12 As You Read This Story Consider How The Images Support And Complemen

Read the short story and answer the following questions

  • Attachment 1
  • Attachment 2
  • Attachment 3
  • Attachment 4

IMPRINTS 12As you read this story .consider how the images supportand complement the textThingsThat Flyby Douglas CouplandI’m sitting hunched over the livingroom coffee table on a Sunday80night , in a daze , having just wokenup from a deep deep sleep on acouch shared with pizza boxes andcrushed plastic cherry yogurt con -cainers . In front of me a TV gameshow is playing on MUTE and myhead rests on top of my hands , as though I am praying , but Iam not ; I am rubbing my eyes and trying to wake up , and myhair is brushing the cabletop which is covered in crumbs and !am thinking to myself that , in spite of everything that has hap -pened in my life , I have never lost the sensation of alwaysbeing on the brink of some magic revelation – that if only Iwould look closely enough at the world , then that magic reve-lation would be mine – if only I could wake up just that little*bit more , then … well – let me describe what happened today .Today went like this : I was up atnoon ; instant coffee ; watched a talksshow ; a game show ; a bit of football ;a religious something – or – other ; thenI turned the TV off . I drifted list-lessly about the house , from silentroom to silent room , spinning the- awheels of the two mountain bikeson their racks in the hallway andstraightening a pile of CDS glued together with spilled OrangeCrush in the living room . I suppose I was trying to pretend !had real things to do , but , well , I didn’t .143

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW

In 1 Henry Iv Some Critics Have Considered The Character Of Prince Hal To Be In

In 1 Henry IV some critics have considered the character of Prince Hal to be in some respects a mean between the characters of Hotspur and Falstaff. In a developed essay, discuss how our knowledge of Hotspur helps define the character of Prince Hal. 

Running Head: KNOWLEDGE OF HOTSPURDEFINE THE CHARACTER OF PRINCEHAL1 Knowledge of Hotspur define The Character of Prince HalName:Institutional Affiliation: KNOWLEDGE OF HOTSPURDEFINE THE…

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
ORDER NOW