Advertisement
Guest User

MergeSort.java

a guest
Oct 30th, 2011
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package multithreadedmergesort;
  2. import java.io.*;
  3.  
  4. /**
  5.  *
  6.  * @author Davidthefat
  7.  */
  8. public class MergeSort
  9. {
  10.     private static String[][] array;
  11.     private static String[] filename;
  12.     private static Thread[] thr;
  13.     public static void main(String[] args)
  14.     {
  15.         filename = new String[args.length];
  16.         array = new String[args.length][];
  17.         for(int i = 0; i < args.length; i++)
  18.         {
  19.             filename[i] = args[i];
  20.             array[i] = new String[Integer.parseInt(filename[i].substring(0, filename[i].indexOf(".")))];
  21.             System.out.println(filename[i] + " " + array[i].length);
  22.         }
  23.         thr = new Thread[args.length];
  24.         try
  25.         {
  26.             for(int i = 0; i < thr.length; i++)
  27.             {
  28.                 thr[i] = new ReadThread(array[i], filename[i]); //Reads in the files
  29.                 thr[i].start();
  30.                 thr[i].join();
  31.             }
  32.             for(int i = 0; i < thr.length; i ++)
  33.             {
  34.                 thr[i] = new Sort(array[i]);
  35.                 thr[i].start();
  36.             }
  37.             for(int i = 0; i < thr.length; i ++)
  38.             {
  39.                 thr[i].join();
  40.                 System.out.println(filename[i] + " Done");
  41.             }
  42.         }
  43.         catch(InterruptedException e)
  44.         {
  45.             System.out.println(e.getMessage());
  46.         }
  47.         try
  48.         {
  49.             BufferedWriter out;
  50.             for(int i = 0; i < array.length; i ++)
  51.             {
  52.                 System.out.println("Printing : " + filename[i]);
  53.                 out = new BufferedWriter(new FileWriter(array[i].length + "output.txt"));
  54.                 for(int k = 0; k < array[i].length; k ++)
  55.                 {
  56.                     out.write(array[i][k]);
  57.                     out.newLine();
  58.                 }
  59.                 out.close();
  60.             }
  61.         }
  62.         catch (IOException e)
  63.         {
  64.             System.out.println(e.getMessage());
  65.         }
  66.         System.out.println("Done");
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement