java 多线程 归并_多线程归并排序(摘自githhub)

packagecom.rationalcoding.sort;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;/***

*@authorPhani

*

* This is implementation of a multi threaded merge sort. First input

* array is divided into small chunks. Each chunk is sorted individually

* using Arrays.sort method which implements insertion sort Then all the

* chunks are merged in bottom up manner by doubling the width after

* each step

**/

public class MultiThreadedMergeSort extendsSequentialMergeSort {private final int DEFAULT_POOL_SIZE = 100;private final int DEFAULT_CHUNK_SIZE = 1000;private ExecutorService pool =Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);private int[] arr;

@SuppressWarnings("rawtypes")

@Overridepublic void sort(int[] arr) {this.arr =arr;//handle null inputs

if (arr == null) {throw new IllegalArgumentException("Input array cannot be null");

}if (arr.length == 0 || arr.length == 1) {//already sorted return

return;

}//width is chunks we are diving the array into

int width =DEFAULT_CHUNK_SIZE;if (width > 1) {//apply insertion sort on chunks and then merge the chunks

ArrayList subTasks = new ArrayList();for (int i = 0; i < arr.length; i = i +width) {int start =i;int end = i +width;if (end >arr.length) {

end=arr.length;

}//add the runnables to pool

subTasks.add(pool.submit(newInsertionSortRunnable(start, end)));

}//wait for the tasks to finish//join all the threads to main thread

for(Future f : subTasks) {try{

f.get();

}catch(InterruptedException e) {

e.printStackTrace();

}catch(ExecutionException e) {

e.printStackTrace();

}

}

}//apply merging on already sorted chunks

for (; width < arr.length; width = width * 2) {

ArrayList tasks = new ArrayList();for (int i = 0; i < arr.length; i = i + 2 *width) {int rStart =i;int rEnd = i + width - 1;int lEnd = i + 2 * width - 1;if (lEnd >=arr.length) {

lEnd= arr.length - 1;

}

tasks.add(pool.submit(newMergeSortRunnable(rStart, rEnd, lEnd)));

}//wait for all threads to finish

for(Future f : tasks) {try{

f.get();

}catch(InterruptedException e) {

e.printStackTrace();

}catch(ExecutionException e) {

e.printStackTrace();

}

}

}

}private class InsertionSortRunnable implementsRunnable {private intstart;private intend;public InsertionSortRunnable(int start, intend) {this.start =start;this.end =end;

}

@Overridepublic voidrun() {

Arrays.sort(arr, start, end);

}

}private class MergeSortRunnable implementsRunnable {private intstart;private intend;private intmid;public MergeSortRunnable(int start, int mid, intend) {this.start =start;this.end =end;this.mid =mid;

}

@Overridepublic voidrun() {if (start < end && mid <=end) {

merge(arr, start, mid, end);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值