JDK7并行计算框架介绍二 Fork/Join开发实例

开发环境:

JDK7.0 + Eclipse3.6 + JUnit4

完整代码:

代码文件一:SortTask.java

package forktest;
import java.util.*;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import junit.*;


public class SortTask extends RecursiveAction {
    final long[] array;
    final int hi;
    final int lo;
    private int THRESHOLD = 30;
    //构造函数
    public SortTask(long[] array)
    {
        this.array = array;
        this.lo = 0;
        this.hi = array.length - 1;        
    }
    //构造函数
    public SortTask(long[] array,int lo,int hi)
    {
        this.array = array;
        this.lo = lo;
        this.hi = hi;        
    }
    //implement RecusiveTask must
    protected void compute() {
        if(hi - lo < THRESHOLD) {
            sequentiallySort(array,lo,hi);             
        }
        else
        {  
            int pivot = partition(array,lo,hi);
            System.out.println("\npivot = " + pivot + ", low = " + lo + ", high = " + hi);
            System.out.println("array" + Arrays.toString(array));
            //注意此处接口有变化,老版本是coInvake,已不支持该接口
            invokeAll(new SortTask(array,lo,pivot-1),new SortTask(array,pivot+1,hi));  
        }
    }
    //任务分割
    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 sequentiallySort(long[] array,int lo,int hi){
        Arrays.sort(array,lo,hi+1);
    }
    //数值交换
    private void swap(long[] array,int i,int j){
        if(i !=j)
        {
            long tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;            
        }
    }
}

代码文件二:TestForkJoinSimple.java

package forktest;
import junit.*;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import forktest.SortTask;

public class TestForkJoinSimple {
    private static final int NARRAY = 16; //For demo only
    long[] array = new long[NARRAY];
    Random rand = new Random();
    
    @Before
    public void setUp() {
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextLong()%100; //For demo only
        }
    }
    
    @Test
    public void testSort() throws Exception {
        ForkJoinTask sort = new SortTask(array);
        ForkJoinPool fjpool = new ForkJoinPool();
        fjpool.submit(sort);
        fjpool.shutdown();
        fjpool.awaitTermination(30, TimeUnit.SECONDS);
        assertTrue(checkSorted(array));
    }
    boolean checkSorted(long[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] > (a[i + 1])) {
                return false;
            }
        }
        return true;
    }
}

运行结果:

pivot = 5, low = 0, high = 15
array[-88, -97, -25, -33, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 4, low = 0, high = 4
array[-88, -97, -25, -33, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 2, low = 0, high = 3
array[-88, -97, -33, -25, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 0, low = 0, high = 1
array[-97, -88, -33, -25, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 1, low = 1, high = 1
array[-97, -88, -33, -25, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 3, low = 3, high = 3
array[-97, -88, -33, -25, -25, 10, 62, 91, 43, 85, 35, 26, 57, 78, 66, 79]

pivot = 13, low = 6, high = 15
array[-97, -88, -33, -25, -25, 10, 62, 43, 35, 26, 57, 78, 66, 79, 91, 85]

pivot = 11, low = 6, high = 12
array[-97, -88, -33, -25, -25, 10, 62, 43, 35, 26, 57, 66, 78, 79, 91, 85]

pivot = 9, low = 6, high = 10
array[-97, -88, -33, -25, -25, 10, 43, 35, 26, 57, 62, 66, 78, 79, 91, 85]

pivot = 6, low = 6, high = 8
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 91, 85]

pivot = 8, low = 7, high = 8
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 91, 85]

pivot = 7, low = 7, high = 7
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 91, 85]

pivot = 10, low = 10, high = 10
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 91, 85]

pivot = 12, low = 12, high = 12
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 91, 85]

pivot = 14, low = 14, high = 15
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 85, 91]

pivot = 15, low = 15, high = 15
array[-97, -88, -33, -25, -25, 10, 26, 35, 43, 57, 62, 66, 78, 79, 85, 91]

注意事项:

JDK版本必须是7.0及以上版本,否则不支持;JUnit必须是4.0及以上版本,否则不支持。

 


作者:张子良
出处:http://www.cnblogs.com/hadoopdev
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

上一篇:基于Hadoop的云盘系统客户端技术难点之一 上传和下载效率优化


下一篇:基于Hadoop的云盘系统客户端技术难点之三 小文件存储优化