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 2 of 2

Thread: thread sorting

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thread sorting

    Hello!!!!
    I want your help for this program!!!
    Sorry for my english!

    I want to create a pro that will sort an array[1024*16], i want to have 3 classes one for sortingthread, one for mergingthread and one for the main.

    I wrote the code for sortingthread and mergingthread class but i have problem for the main class

    /**
     *
     * @author fakie
     */
    public class MergingThread extends Thread {
     
        double[] mergedArray;
        double[] A1;
        double[] A2;
     
        public MergingThread(double[] Array1, double[] Array2) {
     
            A1 = Array1;
            A2 = Array2;
            mergedArray = new double[A1.length + A2.length];
     
        }
     
        public double[] getMergedArray() {
            return mergedArray;
        }
     
        @Override
        public void run() {
     
            int i, j, k, m, n;
     
            i = 0;
            j = 0;
            k = 0;
     
            m = A1.length;
            n = A2.length;
     
            while (i < m && j < n) {
     
                if (A1[i] <= A2[j]) {
     
                    mergedArray[k] = A1[i];
                    i++;
                } else {
     
                    mergedArray[k] = A2[j];
                    j++;
                }
                k++;
            }
     
            if (i < m) {
     
                for (int p = i; p < m; p++) {
     
                    mergedArray[k] = A1[p];
                    k++;
                }
            } else {
     
                for (int p = j; p < n; p++) {
     
                    mergedArray[k] = A2[p];
                    k++;
                }
     
            }
            System.out.println("Merged");
            for(k=0;k<8;k++){
            System.out.println(mergedArray[k]);
            }
        }
    }




    /**
     *
     * @author fakie
     */
    public class SortingThread extends Thread {
     
    	double[] unsrtArray, sortedArray;
        private double threadId;
        private double arraySize;
     
        public SortingThread(double[] unsortedArray) {
            unsrtArray = unsortedArray;
            arraySize = unsortedArray.length;
        }
     
        public double[] getSortedArray() {
            return sortedArray;
        }
     
        public void setThreadId(int id) {
            threadId = id;
        }
     
        @Override
        public void run() {
     
            long startTime = System.currentTimeMillis();
     
            int n = unsrtArray.length;
            for (int pass = 1; pass < n; pass++) {
     
                for (int i = 0; i < n - pass; i++) {
                    if (unsrtArray[i] > unsrtArray[i + 1]) {
     
                    	double temp = unsrtArray[i];
                        unsrtArray[i] = unsrtArray[i + 1];
                        unsrtArray[i + 1] = temp;
     
                    }
                }
            }
            sortedArray = unsrtArray;
     
            long endTime = System.currentTimeMillis();
            System.out.println("Unique Thread Number: " + threadId + ", Array Size: "
                    + arraySize + ", Sorting took: " + (endTime - startTime) + "ms");
     
     
        }
    }



    i want to give in main's args the number of threads

    if i want to sort an array[16] and i want 4 threads

    array[0]-array[3]

    array[4]-array[7]

    array[8]-array[11]

    array[12]-array[15]

    the sortingthread will sort the 4 arrays

    and then the mertgingthread array will combine the 4 arrays

    array[0]-array[3]
    ------------------------------mergingthread(1)
    array[4]-array[7]
    -------------------------------------------------------------mergingthread(3)
    array[8]-array[11]
    ------------------------------mergingthread(2)
    array[12]-array[15]

    The mergingthread(3) it will use the function join to wait to finish mergingthread(1) and mergingthread(2)

    i want only the code for soringthread and mergingthread in class main


    Please if you understand my problem help me!!!!!
    Thanks!!!!!!!!!!!!!


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: thread sorting

    import java.util.*;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.io.*;
     
    public class FileSorter extends Thread {
    	static int pli=16;
    	private static final int MAX_NUMBER = 10;
    	static double[] a1 = new double[8];
    	static double[] a2 = new double[8];
    	static double[][] array;
    	static int nThreads;
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws IOException {
    		nThreads = Integer.parseInt(args[1]);
    		array = new double[nThreads][pli/nThreads];
    		System.out.println(nThreads);
    		Random random = new Random(System.currentTimeMillis());
    		PrintWriter out = new PrintWriter(new FileWriter("numbers.txt"));
    		for(int i = 0; i < pli; i++) {
    			out.println(random.nextInt(MAX_NUMBER));
    			out.flush();
    		}
    		readArray();
     
     
    		for (int i=0;i<nThreads;i++){
    			for (int j=0;j<pli/nThreads;j++){
    				System.out.println(array[i][j]);
    			}
    			System.out.println("");
    		}
     
    		System.out.println("");
     
     
    		SortingThread t[] = new SortingThread[nThreads];       
            for(int i=0; i<nThreads; i++){
                  t[i] = new SortingThread(array[i]);
                  t[i].setThreadId(i);
                  t[i].start();
            }
     
            for(int qq = 0;qq<nThreads;qq++){
            	if(t[qq]!=null){
            		try {
            			t[qq].join();
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
            	}
            }
     
            for (int i=0;i<nThreads;i++){
    			for (int j=0;j<pli/nThreads;j++){
    				System.out.println(array[i][j]);
    			}
    			System.out.println("");
    		}
    		System.out.println("");
     
     
            MergingThread t2[] = new MergingThread[nThreads-1];    
            /*	for(int j=0; j<nThreads-1; j++){
            		t2[j] = new MergingThread(array[0],array[1]);
            		t2[j].start();
            		i+=unsrtArray.length/2;
            		try {
    					t2[j].join();
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
            	}*/
            t2[0] = new MergingThread(array[0],array[1]);
    		t2[0].start();
    		t2[1] = new MergingThread(array[2],array[3]);
    		t2[1].start();
    		for(int qq = 0;qq<nThreads-2;qq++){
            	if(t2[qq]!=null){
            		try {
            			t2[qq].join();
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
            	}
            }
    	//	for(int i1=0; i1<16; i1++){
    			a1=t[0].getSortedArray().length[i];
    			a2=t[1].getSortedArray();
    			for (int j=0;j<8;j++){
    				System.out.println(a2);
    			}
    			System.out.println("");
    	//	}
    		t2[2] = new MergingThread(a1,a2);
    		t2[2].start();
     
     
     
    	}
     
     
    	public static double[][] readArray() throws IOException{
    		Reader r = new BufferedReader(new FileReader("numbers.txt"));
    	    StreamTokenizer stok = new StreamTokenizer(r);
    	    stok.parseNumbers();
    	    stok.nextToken();
    	    int i=0;
    	    int j=0;
    	    while (stok.ttype != StreamTokenizer.TT_EOF) {
    	      if (stok.ttype == StreamTokenizer.TT_NUMBER){
    	    	  array[i][j] = stok.nval;
    	    	  j++;
    	    	  if(j==pli/nThreads){
    	    		  i++;
    	    		  j=0;
    	    	  }
    	    	  stok.nextToken();
    	    	  }
    	    }
    		return array;
    	}
     
    }




    can anybody help me?????? plz!!!!!!

Similar Threads

  1. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  2. Swing JTable sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 2
    Last Post: January 30th, 2010, 08:51 AM
  3. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  4. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM