Fork/Join框架自从在JDK7中引进之后,对并行计算的设计带来了更多便利。
本文使用java原生的排序方法Array.sort单线程排序,和利用Fork/Join框架进行任务分割设计的快速排序进行对比。
首先,使用以下方法构造一个简单的文件样本,目标是生成一个文本文件,10000000行,每行为一个20000以内的随机数:
package sort; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Random; public class GenerateSample { public static void main(String[] args) { File f = new File("/home/nox/java/sort/sample.txt"); FileWriter writer; try { writer = new FileWriter(f, false); Random random1 = new Random(10); for (int i = 0; i < 10000000; i++) { writer.write(String.valueOf(random1.nextInt(20000))); writer.write("\r\n"); } writer.close(); } catch (IOException e) { e.printStackTrace(); } finally { } } }
168890149 Aug 9 21:59 sample.txt
然后,我们将使用下面的普通排序方式对该文件里面的数据进行排序然后输出,因为本文的目标仅仅是简单的测算排序的速度,所以对IO并没有进行任何优化,仅仅是将文件里面的数据进行读取然后排序。故下面的代码也只对排序过程所耗费的时间进行计算。代码如下:
package sort; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArraySort { public static void main(String args[]) { File f = new File("/home/nox/java/sort/sample.txt"); List arrayList = new ArrayList(); try { BufferedReader reader = new BufferedReader(new FileReader(f)); String str = null; while ((str = reader.readLine()) != null) { arrayList.add(Integer.valueOf(str)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } long startTime=System.currentTimeMillis(); Collections.sort(arrayList); long endTime=System.currentTimeMillis(); System.out.println("排序所花时间:"+(endTime-startTime)+"ms"); File f2 = new File("/home/nox/java/sort/original_sorted.txt"); FileWriter writer2; try { writer2 = new FileWriter(f2, false); for (int i = 0; i < arrayList.size(); i++) { writer2.write(String.valueOf(arrayList.get(i))); writer2.write("\r\n"); } writer2.close(); } catch (IOException e) { e.printStackTrace(); } finally { } } }
输出结果为:
排序所花时间:16259ms
然后,我们使用基于 Fork/Join 框架的多线程并行化设计的快速排序进行排序。看看所耗费的时间如何。代码如下:
package sort; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; import java.util.concurrent.RecursiveTask; public class FastSortByForkAndJoin { public static void main(String args[]) { File f = new File("/home/nox/java/sort/sample.txt"); List arrayList = new ArrayList(); try { BufferedReader reader = new BufferedReader(new FileReader(f)); String str = null; while ((str = reader.readLine()) != null) { arrayList.add(Integer.valueOf(str)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } long longArray[] = new long[arrayList.size()]; for( int i =0;i<arrayList.size();i++){ longArray[i] = Long.parseLong(arrayList.get(i).toString()); } ForkJoinPool pool = new ForkJoinPool(); FastSort fastSort = new FastSort(longArray); long startTime=System.currentTimeMillis(); pool.execute(fastSort); while(!fastSort.isDone()){ } long endTime=System.currentTimeMillis(); System.out.println("排序所花时间:"+(endTime-startTime)+"ms"); File f2 = new File("/home/nox/java/sort/fastSorted.txt"); FileWriter writer2; try { writer2 = new FileWriter(f2, false); for (int i = 0; i < longArray.length; i++) { writer2.write(String.valueOf(longArray[i])); writer2.write("\r\n"); } writer2.close(); } catch (IOException e) { e.printStackTrace(); } finally { } } } class FastSort extends RecursiveAction { final long[] array; final int lo; final int hi; private int THRESHOLD = 30; public FastSort(long[] array) { this.array = array; this.lo = 0; this.hi = array.length - 1; } public FastSort(long[] array, int lo, int hi) { this.array = array; this.lo = lo; this.hi = hi; } protected void compute() { if (hi - lo < THRESHOLD) sequentiallySort(array, lo, hi); else { int pivot = partition(array, lo, hi); FastSort left = new FastSort(array, lo, pivot - 1); FastSort right = new FastSort(array, pivot + 1, hi); invokeAll(left, right); } } private int partition(long[] array, int lo, int hi) { long x = array[hi]; int i = lo - 1; for (int j = lo; j < hi; j++) { if (array[j] <= x) { i++; swap(array, i, j); } } swap(array, i + 1, hi); return i + 1; } private void swap(long[] array, int i, int j) { if (i != j) { long temp = array[i]; array[i] = array[j]; array[j] = temp; } } private void sequentiallySort(long[] array, int lo, int hi) { Arrays.sort(array, lo, hi + 1); } }
输出结果为:
排序所花时间:2212ms
总结:在多线程方式下,尽管我们没有对JVM参数,线程数量和单任务中的阀值进行更多的优化,仅仅是一个简单的多线程快速排序,在速度上就已经远胜普通的排序方式,相信如果在数据样本更大的情况下,进行更多的优化,排序的速度势必会有更大的差异。