Results 1 to 5 of 5
  1. #1
    Nknadia is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default sort and merge string data using threads

    Hi,
    I am trying to get string data from 4 files, sort it and merge into one list using threads. My struggle is with the run() method although I do understand how to create the threads.
    I would be able to send the netbeans files and codes if anyone is able to help.
    Thank you




    */
    public static String[] threadedSort(File[] files) throws IOException, InterruptedException{
    String[] data = new String[0];
    String[] threadSortedData = new String[data.length];
    for (File file : files){

    FirstThread mythread = new FirstThread(data);
    Thread thread = new Thread (mythread);
    FirstThread T1 = new FirstThread(left);
    Thread leftside = new Thread(T1);
    FirstThread T2 = new FirstThread(right);
    Thread rightside = new Thread (T2);
    thread.start();
    thread.join();
    data = getData(file);
    leftside.start();
    rightside.start();
    leftside.join();
    rightside.join();
    threadSortedData = MergeSort.merge(threadSortedData, data);
    }

    return threadSortedData;}



    -----------------------------------------------------------------------------------------------

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;





    public class FirstThread implements Runnable {

    String[] data;


    Iterable<File> files;
    File file;



    public FirstThread (String[] data) {
    this.data = data;

    }

    @Override
    public void run(){
    String []data = new String[0];
    try {
    data= Sort.getData(file);
    } catch (IOException ex) {
    Logger.getLogger(FirstThread.class.getName()).log( Level.SEVERE, null, ex);
    }

    String[] threadSortedData = new String[data.length];
    String[] left = new String[data.length / 2];
    String[] right = new String[data.length - left.length];
    data = MergeSort.mergeSort(data);
    threadSortedData = MergeSort.merge(threadSortedData, data);
    }}


    --------------------------------------------------------------------------------------------
    Last edited by Nknadia; 07-27-2013 at 09:01 PM.

  2. #2
    Nknadia is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default sort and merge string data using threads

    Hi,
    I am trying to get string data from 4 files, sort it and merge into one list using threads. My struggle is with the run() method although I do understand how to create the threads.
    I would be able to send the netbeans files and codes if anyone is able to help but some of the code is below
    Thank you



    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;


    public class Sort {
    static String[] left;
    static String[] right;

    /**
    * 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, InterruptedException{
    String[] data = new String[0];
    String[] threadSortedData = new String[data.length];
    for (File file : files){

    FirstThread mythread = new FirstThread(data);
    Thread thread = new Thread (mythread);
    FirstThread T1 = new FirstThread(left);
    Thread leftside = new Thread(T1);
    FirstThread T2 = new FirstThread(right);
    Thread rightside = new Thread (T2);
    thread.start();
    thread.join();
    data = getData(file);
    leftside.start();
    rightside.start();
    leftside.join();
    rightside.join();
    threadSortedData = MergeSort.merge(threadSortedData, data);
    }

    return threadSortedData;}


    /**
    * 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);
    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
    */
    public 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]);

    }
    }

    -----------------------------------------------------------------------------------------------

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;





    public class FirstThread implements Runnable {

    String[] data;


    Iterable<File> files;
    File file;



    public FirstThread (String[] data) {
    this.data = data;

    }

    @Override
    public void run(){
    String []data = new String[0];
    try {
    data= Sort.getData(file);
    } catch (IOException ex) {
    Logger.getLogger(FirstThread.class.getName()).log( Level.SEVERE, null, ex);
    }

    String[] threadSortedData = new String[data.length];
    String[] left = new String[data.length / 2];
    String[] right = new String[data.length - left.length];
    data = MergeSort.mergeSort(data);
    threadSortedData = MergeSort.merge(threadSortedData, data);
    }}


    --------------------------------------------------------------------------------------------

    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;

    }

    }
    ------------------------------------------------------------------------------------------------------------------------------

    import java.io.File;
    import java.io.IOException;

    /**
    * The class SortTest is used to test the threaded and non-threaded
    * sort methods. This program will call each method to sort the data
    * contained in the four test files. This program will then test the
    * results to ensure that the results are sorted in ascending order.
    *
    * Simply run this program to verify that you have correctly implemented
    * the threaded sort method. The program will not verify if your sort
    * uses threads, but will verify if your implementation correctly
    * sorted the data contained in the four files.
    *
    * There should be no reason to make modifications to this class.
    */
    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
    long startTime = System.nanoTime();
    String[] sortedData = Sort.sort(files);
    long stopTime = System.nanoTime();
    double elapsedTime = (stopTime - startTime) / 1000000000.0;

    // Test to ensure the data is sorted
    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.");
    System.out.println("Sort.sort took " + elapsedTime + " seconds to read and sort the data.");

    // Run Sort.threadedSort on the files and test to ensure the data is sorted
    startTime = System.nanoTime();
    String[] threadSortedData = Sort.threadedSort(files);
    stopTime = System.nanoTime();
    double threadedElapsedTime = (stopTime - startTime)/ 1000000000.0;

    // Test to ensure the data is sorted
    if (sortedData.length != threadSortedData.length) {
    System.out.println("The data return by Sort.threadedSort is missing data");
    throw new java.lang.IllegalStateException("The data returned by Sort.threadedSort is not sorted");
    }
    for (int i=0; i<threadSortedData.length-1; i++) {
    if (threadSortedData[i].compareTo(threadSortedData[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.");
    System.out.println("Sort.threadedSort took " + threadedElapsedTime + " seconds to read and sort the data.");


    }

    }
    Last edited by Nknadia; 07-27-2013 at 08:58 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: sort and merge string data using threads

    Quote Originally Posted by Nknadia View Post
    I would be able to send the netbeans files and codes
    No, don't do that. Instead, create and post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    Before you do that, go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Nknadia is offline Member
    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Default Re: sort and merge string data using threads

    Ok Thank you for the information I will read the guide.
    But I just added some of the code to my initial message

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: sort and merge string data using threads

    Kindly go through the links I posted and edit your posts accordingly. Also go through the Forum Rules, particularly the second paragraph.

    I've merged two threads here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Merge sort
    By Aivy in forum New To Java
    Replies: 3
    Last Post: 12-14-2011, 12:29 PM
  2. iterative merge sort
    By baf06 in forum New To Java
    Replies: 5
    Last Post: 10-23-2011, 10:17 PM
  3. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  4. Merge Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  5. Merge Sort Help
    By Hollywood in forum New To Java
    Replies: 5
    Last Post: 01-30-2008, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •