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

  介绍

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

Java基本数据类型有哪些

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

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

<>之前,   import  java.util.Arrays;   ,   public  class  JavaDemo  {   ,   ,,,public  static  int[],加勒比海盗;   ,   ,,,public  static  void  main (String [], args), {   ,,,,,,,//随机生成数的值   ,,,,,,,arr =, new  int [100];   ,,,,,,,for  (int 小姐:=,0;,小姐:& lt;, arr.length;,我+ +),{   ,,,,,,,,,,,arr[我],=,(int), (math . random (), *, 1000);   ,,,,,,,}   ,,,,,,,new  JavaDemo () .doSort (0,, arr.length 作用;1);   ,,,,,,,for  (int  element : arr), {   ,,,,,,,,,,,System.out.println(元素);   ,,,,,,,}   ,,,}   ,   ,,,public  void  doSort (int 低,int 高),{   ,,,,,,,if  (low  & lt;,高),{   ,,,,,,,,,,,int  index =,快速排序(低,高);//实际的排序流程   ,,,,,,,,,,,doSort(低,,index 作用;1);   ,,,,,,,,,,,doSort(时间+ index  1,高);   ,,,,,,,}   ,,,}   ,   ,,,public  int 快速排序(int 我,int  j), {   ,,,,,,,int  key =, arr[我];//基准值   ,,,,,,,while (小姐:& lt;, j), {   ,,,,,,,,,,,//找出第一个右边要交换的   ,,,,,,,,,,,while (小姐:& lt;, j ,,, arr [j],祝辞=,键),j——;   ,,,,,,,,,,,if (小姐:& lt;, j), arr[我],=,arr [j];   ,,,,,,,,,,,//找出第一个左边要交换的   ,,,,,,,,,,,while (小姐:& lt;, j ,,, arr[我],& lt;=,键),我+ +;   ,,,,,,,,,,,if (小姐:& lt;, j), arr [j],=, arr[我];   ,,,,,,,}   ,,,,,,,//,我==,j的情况   ,,,,,,,arr[我],=,关键;   ,,,,,,,return 我;   ,,,}   }

<强> 2。数据分段

//根据我们设立的线程数来分段
  for  (int 小姐:=,0;,小姐:& lt;, threadNum;,我+ +),{
  ,,,int [], temp =, Arrays.copyOfRange(加勒比海盗,小姐:*,arr.length /, threadNum,,
  ,,,,,,(小姐:+,- 1),*,arr.length /, threadNum);
  ,,,//theadNum就是线程数
  }
  快排线程:采用可调用的接口,可以有返回值
  package 进步;/*
  ,* @Author: Gnight
  ,* @Date: 2020/12/17 十九10
  ,* @Description:
  ,,,多线程实现快排
  ,*/,
  import  java.util.concurrent.Callable;
  import  java.util.concurrent.CountDownLatch;
  ,//快排多线程
  public  class  sortThread  implements  Callable< int[]祝辞,{
  ,
  ,,,private  int[],加勒比海盗;
  ,,,private  int 低;
  ,,,private  int 高;
  ,,,private  CountDownLatch 计算;
  ,
  ,,,public  sortThread (int[],加勒比海盗,,int 低,,int 高,CountDownLatch 计数),{
  ,,,,,,,this.arr =,加勒比海盗;
  ,,,,,,,this.low =,低;
  ,,,,,,,this.high =,高;
  ,,,,,,,this.count =,计数;
  ,,,}
  ,
  ,,,public  int[],叫(),throws  Exception  {
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

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