Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Need Help with Sorting using thread sorting, How do I use a thread?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with Sorting using thread sorting, How do I use a thread?

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
     
    public class Sort {
     
    public static String[] threadedSort (File[] files) throws IOException { 
    throw new java.lang.IllegalStateException ("Method not implemented");
    }
     
    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);
         MergeSort.merge(sortedData, data);
    }
     
       return sortedData;
     
    }
     
    private static String[] getData(File file) throws IOException  {
     
      ArrayList<String> data = new ArrayList<String>();
      BufferedReader in = new BufferedReader 9new FileReader(file));
     
    while  (true) {
    String line = in.readLine();
    if (line ==null) {
    break;
    }
    else {
    data.add(line);
    }
    }
     
    in.close();
    return data.toArray(new String[0]);
    }
    }
    Last edited by Matts4948; March 10th, 2014 at 09:35 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    A sort would be done the same way in a thread the code explicitly starts as in a thread started by the jvm.
    See the API doc for the Thread class for how to use a thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Where is this API doc?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Could you please help me with this, I'm really not that good at this.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Start by copying a working example from the tutorial.
    Make a simple change to the code, compile and execute it. If there are problems, copy the code and the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
     
    public class Sort {
     
      /**
       * 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 {
        throw new java.lang.IllegalStateException("Method not implemented");
      }
     
     
      /**
       * 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);
          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]);
     
      }
    }

    Result

    run:
    The data returned by Sort.sort is sorted
    Exception in thread "main" java.lang.IllegalStateException: Method not implemented
    at Sort.threadedSort(Sort.java:21)
    at SortTest.main(SortTest.java:22)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Exception in thread "main" java.lang.IllegalStateException: Method not implemented
    at Sort.threadedSort(Sort.java:21)
    at SortTest.main(SortTest.java:22)
    The above exception comes from the following statement in the code:
        throw new java.lang.IllegalStateException("Method not implemented");

    Ask the author of that code why that statement is there and what needs to be done about it.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    These are the other files

    public class MergeSort {
     
      // The mergeSort method returns a sorted copy of the
      // String objects contained in the String array data.
      /**
       * Sorts the String objects using the merge sort algorithm.
       * 
       * @param data the String objects to be sorted
       * @return the String objects sorted in ascending order
       */
      public static String[] mergeSort(String[] data) {
     
        if (data.length > 1) {
          String[] left = new String[data.length / 2];
          String[] right = new String[data.length - left.length];
          System.arraycopy(data, 0, left, 0, left.length);
          System.arraycopy(data, left.length, right, 0, right.length);
     
          left = mergeSort(left);
          right = mergeSort(right);
     
          return merge(left, right);
     
        }
        else {
          return data;
        }
     
      }
     
      /**
       * The merge method accepts two String arrays that are assumed
       * to be sorted in ascending order. The method will return a
       * sorted array of String objects containing all String objects
       * from the two input collections.
       * 
       * @param left a sorted collection of String objects
       * @param right a sorted collection of String objects
       * @return a sorted collection of String objects
       */
      public static String[] merge(String[] left, String[] right) {
     
        String[] data = new String[left.length + right.length];
     
        int lIndex = 0;
        int rIndex = 0;
     
        for (int i=0; i<data.length; i++) {
          if (lIndex == left.length) {
            data[i] = right[rIndex];
            rIndex++;
          }
          else if (rIndex == right.length) {
            data[i] = left[lIndex];
            lIndex++;
          }
          else if (left[lIndex].compareTo(right[rIndex]) < 0) {
            data[i] = left[lIndex];
            lIndex++;
          }
          else {
            data[i] = right[rIndex];
            rIndex++;
          }
        }
     
        return data;
     
      }
     
    }


    --- Update ---

    And this one

     
    import java.io.File;
    import java.io.IOException;
     
    public class SortTest {
     
      public static void main(String[] args) throws IOException {
     
        File[] files = {new File("enable1.txt"), new File("enable2k.txt"), new File("lower.txt"), new File("mixed.txt")};
     
        // Run Sort.sort on the files and test to ensure the data is sorted
        String[] sortedData = Sort.sort(files);
        for (int i=0; i<sortedData.length-1; i++) {
          if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
            System.out.println("The data returned by Sort.sort is not sorted");
            throw new java.lang.IllegalStateException("The data returned by Sort.sort is not sorted");
          }
        }
        System.out.println("The data returned by Sort.sort is sorted");
     
        // Run Sort.threadedSort on the files and test to ensure the data is sorted
        String[] threadSortedData = Sort.threadedSort(files);
        for (int i=0; i<sortedData.length-1; i++) {
          if (sortedData[i].compareTo(sortedData[i+1]) > 0) {
            System.out.println("The data return by Sort.threadedSort is not sorted");
            throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
          }
        }
        System.out.println("The data returned by Sort.threadedSort is sorted");
     
     
      }
     
    }


    --- Update ---

    I have to 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.
    Last edited by Matts4948; March 10th, 2014 at 11:40 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    I have to improve the performance of the Java program by adding threads to the Sort.java
    Have you been able to compile and execute the current code and have it do a sort?
    What "improvement" do you expect by using threads? Does the "improvement" depend on the type of computer the code is executed on? If the threads can not be executed in parallel, then using threads might not improve the performance.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    No, I haven't been able to compile or execute the code because I get this error:

    run:
    The data returned by Sort.sort is sorted
    Exception in thread "main" java.lang.IllegalStateException: Method not implemented
    at Sort.threadedSort(Sort.java:21)
    at SortTest.main(SortTest.java:22)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

    The improvement has to make it faster and it doesn't matter on what type of computer it's on.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Improving an algorithm doesn't mean using threads.

    That exception is because the code chooses to throw it. The code must be rewritten so that exception is not thrown.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    So how would I rewrite it?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Comment out the throws statement to keep it from throwing the exception.

    There are many types of sorts. Has your instructor told you which one to use and how its parts can be separated to run on separate threads?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    How do I comment out the throws statement? She hasn't told us anything because this is an online course and she really doesn't say anything. She just gives a chapter to read and gives us a link and expects us to write a code.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    To comment out a statement:
    put // in column 1
    or
    wrap the statement: /* THE STATEMENT */
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    So where exactly would I comment out, you have to know I'm new at this.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    If commenting out a statement is too hard, then this whole assignment will be too hard.

    The line number where the exception is thrown is shown in the error message:
    Exception in thread "main" java.lang.IllegalStateException: Method not implemented
    at Sort.threadedSort(Sort.java:21)
    at SortTest.main(SortTest.java:22)
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Ok, I finally received this.

    run:
    The data returned by Sort.sort is sorted
    The data returned by Sort.threadedSort is sorted
    BUILD SUCCESSFUL (total time: 1 second)


    But shouldn't it be doing something?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    But shouldn't it be doing something?
    What are you expecting?

    Next you need to verify that the sort is working as desired.
    To see if it is, add some println() statements that print out the contents of the arrays that are used to hold the data. Add several println()s to show the contents before and after the sort.
    An easy way to see array's contents is to use the Array class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    Where am I supposed to put this?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with Sorting using thread sorting, How do I use a thread?

    Put it after any statement that changes the value of an array variable so you can see what value was assigned to that variable.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to categorise a daemon thread to specific thread in java?
    By rajasekhar_b in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2014, 07:55 AM
  2. Replies: 1
    Last Post: September 24th, 2013, 04:18 PM
  3. Creating a Thread from a Thread
    By angstrem in forum Threads
    Replies: 11
    Last Post: May 29th, 2013, 09:31 AM
  4. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  5. thread sorting
    By thanos_ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 06:23 PM