栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java多线程排序使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多线程排序使用

本教程操作环境:windows7系统、java10版,DELL G3电脑。

1.先试一下我们不用多线程的情况,以快速排序为例

package advance1;
 
import java.util.Arrays;
 
public class JavaDemo {
 
    public static int[] arr;
 
    public static void main(String[] args) {
        //随机生成数值
        arr = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 1000);
        }
        new JavaDemo().doSort(0, arr.length - 1);
        for (int element : arr) {
            System.out.println(element);
        }
    }
 
    public void doSort(int low, int high) {
        if (low < high) {
            int index = quickSort(low, high);//实际的排序流程
            doSort(low, index - 1);
            doSort(index + 1, high);
        }
    }
 
    public int quickSort(int i, int j) {
        int key = arr[i];//基准值
        while (i < j) {
            //找出第一个右边要交换的
            while (i < j && arr[j] >= key) j--;
            if (i < j) arr[i] = arr[j];
            //找出第一个左边要交换的
            while (i < j && arr[i] <= key) i++;
            if (i < j) arr[j] = arr[i];
        }
        // i== j的情况
        arr[i] = key;
        return i;
    }
}

2.数据分段

//根据我们设立的线程数来分段
for (int i = 0; i < threadNum; i++) {
    int[] temp = Arrays.copyOfRange(arr, i * arr.length / threadNum, 
       	(i + 1) * arr.length / threadNum);
    //theadNum就是线程数
}
快排线程:采用Callable接口,可以有返回值
package advance1;
 
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
 
//快排多线程
public class sortThread implements Callable {
 
    private int[] arr;
    private int low;
    private int high;
    private CountDownLatch count;
 
    public sortThread(int[] arr, int low, int high, CountDownLatch count) {
        this.arr = arr;
        this.low = low;
        this.high = high;
        this.count = count;
    }
 
    public int[] call() throws Exception {
        System.out.println("线程 " + Thread.currentThread().getName() + " 开始");
        doSort(low, high);
        int[] res = new int[high - low + 1];
        int index = 0;
        for (int i = low; i < high + 1; i++) {
            res[index++] = arr[i];
        }
        try {
            return arr;
        } finally {
            count.countDown();
            System.out.println("线程 " + Thread.currentThread().getName() + " 结束");
        }
    }
 
    public void doSort(int low, int high) {
        if (low < high) {
            int index = quickSort(low, high);//实际的排序流程
            doSort(low, index - 1);
            doSort(index + 1, high);
        }
    }
 
    public int quickSort(int i, int j) {
        int key = arr[i];//基准值
        while (i < j) {
            //找出第一个右边要交换的
            while (i < j && arr[j] >= key) j--;
            if (i < j) arr[i] = arr[j];
            //找出第一个左边要交换的
            while (i < j && arr[i] <= key) i++;
            if (i < j) arr[j] = arr[i];
        }
        // i== j的情况
        arr[i] = key;
        return i;
    }
}

3.创建多线程,使用CountDownLatch保证前面都完成后再对数据段合并

try {
    CountDownLatch count = new CountDownLatch(threadNum);
    for (int i = 0; i < threadNum; i++) {
        int[] temp = Arrays.copyOfRange(arr, i * arr.length / threadNum, (i + 1) * arr.length / threadNum);
        Future future = pool.submit(new sortThread(temp, 0,
                        temp.length - 1, count));
        res[i] = future.get();
    }
    
    count.await();
    //这里排序亦可使用多线程
    int[] m1 = merge(res[0], res[1]);
    int[] m2 = merge(res[2], res[3]);
    arr = merge(m1, m2);
} catch (Exception e) {
    e.printStackTrace();
}

以上就是java多线程排序的方法,大家可以跟着小编的流程尝试下。

本文来源于网络,如有雷同联系作者修改。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/15361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号