Improve the performance of the Java program by adding threads to the Sort.java file. Implement the threadedSort() method within the Sort class. 1

[ad_1]

Improve the act of the Java program by adding continuitys to the Sort.java file. Appliance the threadedSort() direct among the Sort class. (Reuse any of the massive directs by business them as compulsory from your threadedSort method. You may add affixed directs to the Sort class, if compulsory.)

Document your segregation as a blunt pamphlet (1 page).

Main Improve (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 appliance this direct. The direct should beseech one or

013   * more continuitys to learn and collocate the postulates from the collation of Files.

014   * The direct should produce a collocateed catalogue of all of the String postulates

015   * contained in the improves.

016   *

017   * @param improves

018   * @return

019   * @throws IOException

020   */

021  public static String[] continuityedSort(File[] improves) throws IOException

022  {

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

024      SortThread[] collocateThreads = 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 (SortThlearn thlearn : collocateThreads) {

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 applianceed");

040  }

041   

042  private static class SortThread extends Thlearn {

043      private File improve;

044      private String[] postulates;

045 

046       

047      public SortThread(File improve) {

048          this.improve = improve;

049      }

050       

051      @Override

052      public void run() {

053          try {

054              postulates = Sort.getData(file);

055              for (int i = 0; i

056                  //Thread.sleep(100);               

057                 //I commented this out consequently it was dry-rot time

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

059                  //I commented this out consequently I didn't lack to vault the undiminished improves

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 draw-up of improves, this direct procure produce a collocateed

070   * catalogue of the String postulates contained in each of the improves.

071   *

072   * @param improves the improves to be learn

073   * @produce the collocateed postulates

074   * @throws IOException thrown if any falsitys take-place learning the improve

075   */

076  public static String[] collocate(File[] improves) throws IOException {

077 

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

079 

080    for (File improve : improves) {

081      String[] postulates = getData(file);

082      postulates = MergeSort.mergeSort(data);

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

084    }

085 

086    return sortedData;

087 

088  }

089  /**

090   * This direct procure learn in the string postulates from the specified

091   * improve and produce the postulates as an draw-up of String objects.

092   *

093   * @param improve the improve containing the String postulates

094   * @produce String draw-up containing the String postulates

095   * @throws IOException thrown if any falsitys take-place learning the improve

096   */

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

098 

099    ArrayList postulates = new ArrayList();

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

101    // Learn the postulates from the improve until the end of improve is reached

102    while (true) {

103      String continuity = in.readLine();

104      if (continuity == null) {

105        // the end of improve was reached

106        break;

107      }

108      else {

109        data.add(line);

110      }

111    }

112    //Close the input drift and produce the postulates

113    in.close();

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

115 

116  }

117}

Mergesort.java:

01public class MergeSort {

02   

03  // The addSort direct produces a collocateed delineation of the

04  // String objects contained in the String draw-up postulates.

05  /**

06   * Sorts the String objects using the add collocate algorithm.

07   *

08   * @param postulates the String objects to be collocateed

09   * @produce the String objects collocateed in ascending direct

10   */

11  public static String[] addSort(String[] postulates) {

12 

13    if (data.protraction > 1) {

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

15      String[] suitable = new String[data.protraction - left.length];

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

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

18     

19      left = addSort(left);

20      suitable = addSort(right);

21     

22      return merge(left, suitable);

23       

24    }

25    else {

26      return data;

27    }

28     

29  }

30   

31  /**

32   * The add direct accepts two String draw-ups that are assumed

33   * to be collocateed in ascending direct. The direct procure produce a

34   * collocateed draw-up of String objects containing all String objects

35   * from the two input collations.

36   *

37   * @param left a collocateed collation of String objects

38   * @param suitable a collocateed collation of String objects

39   * @produce a collocateed collation of String objects

40   */

41  public static String[] add(String[] left, String[] suitable) {

42     

43    String[] postulates = new String[left.protraction + suitable.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] = suitable[rIndex];

51        rIndex++;

52      }

53      else if (rIndex == suitable.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] = suitable[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 collocate SortTest is used to experience the continuityed and non-threaded

06 * collocate directs. This program procure persuade each direct to collocate the postulates

07 * contained in the disgusting experience improves. This program procure then experience the

08 * results to secure that the results are collocateed in ascending direct.

09 *

10 * Simply run this program to substantiate that you entertain appropriately applianceed

11 * the continuityed collocate direct. The program procure not substantiate if your collocate

12 * uses continuitys, but procure substantiate if your applianceation appropriately

13 * collocateed the postulates contained in the disgusting improves.

14 *

15 * There should be no deduce to mould modifications to this collocate.

16 */

17public class SortTest {

18   

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

20     

21    File[] improves = {new File("enable1.txt"), new File("enable2k.txt"), newFile("lower.txt"), new File("mixed.txt")};

22 

23    // Run Sort.collocate on the improves

24    long startTime = System.nanoTime();

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

26    long stopTime = System.nanoTime();

27    double elapsedTime = (stopTime - set-on-footTime) / 1000000000.0;

28     

29    // Experience to secure the postulates is collocateed

30    for (int i=0; i

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

32        System.out.println("The postulates produceed by Sort.collocate is not collocateed.");

33        throw new java.lang.IllegalStateException("The postulates produceed by Sort.collocate is not collocateed");

34      }

35    }

36    System.out.println("The postulates produceed by Sort.collocate is collocateed.");

37    System.out.println("Sort.collocate took " + elapsedTime + " seconds to learn and collocate the postulates.");

38     

39    // Run Sort.threadedSort on the improves and experience to secure the postulates is collocateed

40    startTime = System.nanoTime();

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

42    stopTime = System.nanoTime();

43    double threadedElapsedTime = (stopTime - set-on-footTime)/ 1000000000.0;

44     

45    // Experience to secure the postulates is collocateed

46    if (sortedData.protraction != continuitySortedData.length) {

47      System.out.println("The postulates produce by Sort.threadedSort is detriment postulates");

48      throw new java.lang.IllegalStateException("The postulates produceed by Sort.threadedSort is not collocateed");

49    }

50    for (int i=0; i

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

52        System.out.println("The postulates produce by Sort.threadedSort is not collocateed");

53        throw new java.lang.IllegalStateException("The postulates produceed by Sort.threadedSort is not collocateed");

54      }

55    }

56    System.out.println("The postulates produceed by Sort.threadedSort is collocateed.");

57    System.out.println("Sort.threadedSort took " + continuityedElapsedTime + " seconds to learn and collocate the postulates.");

58     

59     

60  }

61   

62}

The falsity am getting:

run:The postulates produceed by Sort.collocate is collocateed.Sort.collocate took 2.072120353 seconds to learn and collocate the postulates.The postulates produce by Sort.threadedSort is detriment postulatesException in thlearn "main" java.lang.IllegalStateException: The postulates produceed by Sort.threadedSort is not collocateedat SortTest.main(SortTest.java:49)Java Result: 1BUILD SUCCESSFUL (completion time: 3 seconds)


[ad_2]
Source incorporate