zoukankan      html  css  js  c++  java
  • 多线程归并排序(摘自githhub)

    package com.rationalcoding.sort;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    /**
     * 
     * @author Phani
     * 
     *         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 extends SequentialMergeSort {
    
        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")
        @Override
        public 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<Future> subTasks = new ArrayList<Future>();
                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(new InsertionSortRunnable(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<Future> tasks = new ArrayList<Future>();
                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(new MergeSortRunnable(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 implements Runnable {
            private int start;
            private int end;
    
            public InsertionSortRunnable(int start, int end) {
                this.start = start;
                this.end = end;
            }
    
            @Override
            public void run() {
                Arrays.sort(arr, start, end);
            }
    
        }
    
        private class MergeSortRunnable implements Runnable {
    
            private int start;
            private int end;
            private int mid;
    
            public MergeSortRunnable(int start, int mid, int end) {
                this.start = start;
                this.end = end;
                this.mid = mid;
            }
    
            @Override
            public void run() {
                if (start < end && mid <= end) {
                    merge(arr, start, mid, end);
                }
    
            }
    
        }
    
    }
  • 相关阅读:
    【AMAD】django-channels -- 为Django带来异步开发
    【AMAD】django-crispy-forms -- 不要再重复编写Django Form了!
    【AMAD】django-compressor -- 将JS和CSS文件压缩为一个缓存文件
    【AMAD]django-filter -- 一个通用的,基于用户选择的Django Queryset 过滤系统
    【AMAD】django-taggit -- 一个简单的,通用的django tagging模块
    【AMAD】django-debug-toolbar -- 一个可配置的panel,展示当前request/response的debug信息
    【amad】cookiecutter -- 一个命令行工具,使用项目模版来构建项目
    十步学习法 -- 来自<<软技能>>一书的学习方法论
    第八篇、SVN在Mac上使用
    第七篇、Nginx Install On Mac
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3790409.html
Copyright © 2011-2022 走看看