温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Java中实现多线程排序

发布时间:2021-04-22 17:30:36 来源:亿速云 阅读:578 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关怎么在Java中实现多线程排序,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

java基本数据类型有哪些

Java的基本数据类型分为:1、整数类型,用来表示整数的数据类型。2、浮点类型,用来表示小数的数据类型。3、字符类型,字符类型的关键字是“char”。4、布尔类型,是表示逻辑值的基本数据类型。

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

  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;/*  * @Author: Gnight  * @Date: 2020/12/17 19:10  * @Description:     多线程实现快排  */   import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch;   //快排多线程 public class sortThread implements Callable<int[]> {       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<int[]> future = pool.submit(new sortThread(temp, 0,                         temp.length - 1, count));         res[i] = future.get();     }     /*     使用CountDownLatch来保证前面线程都排序完,然后对排序完的升序数组合并     */     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中实现多线程排序了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI