怎么在Java中对多线程进行排序?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
1.先试一下我们不用多线程的情况,以快速排序为例
package advance1;/*
* @Author: Gnight
* @Date: 2020/12/17 23:32
* @Description:
单线程的快速排序,太多数据栈会溢出
*/
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中对多线程进行排序的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.py.cn/java/jichu/22152.html