java - Java 中的多线程快速排序

标签 java multithreading

你能帮我用 Java 实现多线程 QuickSort 吗?

我有下一个问题: 1 个线程的快速排序的工作时间比 2 或 3 个线程的快速排序要少。我做错了什么?

我为 worker 编写了下一个代码:

public class QuickSortWorker extends Thread {

    private int workerId;
    private double[] array;
    private int low;
    private int high;

    public QuickSortWorker(double[] array, int low, int high) {
        this.array = array;
        this.low = low;
        this.high = high;
        this.workerId = WorkerManager.workersCount++;
    }

    public void run() {
        System.out.println("Run new worker. ID: " + this.workerId);
        System.out.println("Worker status: " + this.isAlive());

        this.quickSort(this.array, this.low, this.high);

        System.out.println("Stop worker. ID: " + this.workerId);
        WorkerManager.workersCount--;
    }

    protected void quickSort(double[] array, int low, int high) {
        /*int i = low, j = high;
        double pivot = array[low + (high - low) / 2];

        while (i <= j) {
            while (array[i] < pivot) {
                i++;
            }

            while (array[j] > pivot) {
                j--;
            }

            if (i <= j) {
                this.exchange(array, i, j);
                i++;
                j--;
            }
        }

        if (low < j) {
            quickSort(array, low, j);
        }

        if (i < high) {
            sort(array, i, high);
        }*/

        int len = high - low + 1;
        if (len <= 1) {
            return;
        }

        double pivot = array[low + (high - low) / 2];
        exchange(array, low + (high - low) / 2, high);

        int storeIndex = low;
        for (int i = low; i < high; i++) {
            if (array[i] < pivot) {
                exchange(array, i, storeIndex);
                storeIndex++;
            }
        }

        exchange(array, storeIndex, high);

        sort(array, low, storeIndex - 1);
        quickSort(array, storeIndex + 1, high);
    }

    protected void exchange(double[] array, int i, int j) {
        double tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    protected void sort(double[] array, int low, int high) {
        if (WorkerManager.workersCount < WorkerManager.WORKERS_LIMIT) {
            try {
                WorkerManager.createWorker(array, low, high);
            } catch (InterruptedException e) {
                System.out.println("QuickSort worker was down. Reason: " + e.getMessage() + "\n");
                e.printStackTrace();
            }
        } else {
            this.quickSort(array, low, high);
        }
    }
}

WorkerManager 的下一个代码:

public class WorkerManager {

    public static final int MAX_WORKER_COUNT = 10;
    public static int WORKERS_LIMIT = 0;
    public static int workersCount = 0;

    private double _startTime = 0;
    private double _stopTime = 0;
    private double _workTime = 0;

    public void createTask(double[] data, int workerCount) throws WorkerManagerException {  
        if (workerCount > MAX_WORKER_COUNT) {
            throw new WorkerManagerException("Worker count cant be more than " + MAX_WORKER_COUNT);
        }

        try {
            WORKERS_LIMIT = workerCount;
            this.startTimer();
            WorkerManager.createWorker(data, 0, data.length - 1);
            this.stopTimer();

            System.out.println("\nWorktime: " + this.getWorkTime() + " milliseconds.");
        } catch (InterruptedException e) {
            System.out.println("QuickSort worker was down. Reason: " + e.getMessage() + "\n");
            e.printStackTrace();
        }
    }

    public static void createWorker(double[] array, int low, int high) throws InterruptedException {
        QuickSortWorker worker = new QuickSortWorker(array, low, high);
        worker.start();
        worker.join();
    }

    protected void startTimer() {
        this._startTime = System.currentTimeMillis(); 
    }

    protected void stopTimer() {
        this._stopTime = System.currentTimeMillis();
    }

    private void setWorkTime() {
        this._workTime = (this._startTime == 0)
                ? 0
                : this._stopTime - this._startTime;
    }

    private double getWorkTime() {
        if (this._workTime == 0) {
            this.setWorkTime();
        }
        return this._workTime;
    }
}

最佳答案

您在主线程中创建工作线程,一旦开始,您就会阻塞主线程(加入)并等待工作线程完成:

 QuickSortWorker worker = new QuickSortWorker(array, low, high);
 worker.start();
 worker.join();

这不会为您提供所需的并行性。在任何给定时间都只有一个线程在工作 - 这就是没有性能提升的原因。

关于java - Java 中的多线程快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946144/

相关文章:

c++ - 可实例化类的线程过程?

c++ - 如果线程分离,我是否需要终止它?

c++ - 如何在 C++ 中正确创建线程类的子类(std::thread 的子类)

java - 尝试学习spring或struts框架的前提是什么?

java - 了解开关行为

java - 如何创建一个在数字而不是字符串中写入日期的 JsonbCong?

java - 安卓 :Retrieving image from parse to an android activity

java - 使用 ThreadLocal 的最佳方法是什么 - 通过静态或非静态方法?

c# - 在关闭主窗体之前关闭侧线程

java - 简单的一种方式挂起线程