Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JButton停止线程_Java_Multithreading_Swing_Sorting_Japplet - Fatal编程技术网

Java 使用JButton停止线程

Java 使用JButton停止线程,java,multithreading,swing,sorting,japplet,Java,Multithreading,Swing,Sorting,Japplet,我有一个对数组进行排序的小程序。在一个JPanel上,它使用用户选择的排序对一个数组进行排序,在另一个JPanel上,它同时使用所有四个数组,每个数组都在线程中 代码同时为4 private class startAH implements ActionListener { public void actionPerformed(ActionEvent e){ allArrays.clear(); bubble.clear(); sele

我有一个对数组进行排序的小程序。在一个JPanel上,它使用用户选择的排序对一个数组进行排序,在另一个JPanel上,它同时使用所有四个数组,每个数组都在线程中

代码同时为4

private class startAH implements ActionListener {
    public void actionPerformed(ActionEvent e){

        allArrays.clear();
        bubble.clear();
        selection.clear();
        quick.clear();
        merge.clear();

        startBut.setEnabled(false);
        threadStart = true;

        loopJump = (int) spinner.getValue();
        loopFinish = loopJump*10;

        currentPercentBubble =0;
        currentPercentSelection =0;
        currentPercentMerge =0;
        currentPercentQuick =0;

        bubbleBool=false;
        selectionBool=false;
        quickBool=false;
        mergeBool=false;
        saveBut.setEnabled(false);

        iBubble=0;
        iSelection=0;
        iQuick=0;
        iMerge=0;

        new Thread(new Runnable(){
            public void run(){
                for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
                {

                    Long elapsedTime=(long) 0;
                    Long currentTime;



                    for(int innerLoop = 0; innerLoop < 10; innerLoop++){

                        long startTime = System.currentTimeMillis();
                        try {
                            int[] intArray = new int[outLoop];
                            sort.fillArray(intArray);
                            sort.bubbleSort(intArray);
                        } catch (InterruptedException e) {
                        }
                        long endTime = System.currentTimeMillis();
                        currentTime = endTime-startTime;
                        elapsedTime +=currentTime;
                        currentPercentBubble++;
                        repaint();
                    }

                    long finalTime = (long) (elapsedTime/10);

                    String[] current = new String[2];
                    current[0]=""+outLoop;
                    current[1]=""+finalTime;
                    bubble.add(iBubble, current);

                    iBubble++;

                }
                allArrays.add(3, bubble);
                bubbleBool = true;
                saveCheck();
                repaint();
            }
        }).start();

        new Thread(new Runnable(){
            public void run(){
                for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
                {

                    Long elapsedTime=(long) 0;
                    Long currentTime;

                    for(int innerLoop = 0; innerLoop < 10; innerLoop++){
                        long startTime = System.currentTimeMillis();
                        try {
                            int[] intArray = new int[outLoop];
                            sort.fillArray(intArray);
                            sort.selectionSort(intArray);
                        } catch (InterruptedException e) {

                        }
                        long endTime = System.currentTimeMillis();
                        currentTime = endTime-startTime;
                        elapsedTime +=currentTime;
                        currentPercentSelection++;
                        repaint();

                    }

                    long finalTime = (long) (elapsedTime/10);

                    String[] current = new String[2];
                    current[0]=""+outLoop;
                    current[1]=""+finalTime;
                    selection.add(iSelection, current);

                    iSelection++;

                }
                allArrays.add(2, selection);
                selectionBool = true;
                saveCheck();
                repaint();
            }
        }).start();

        new Thread(new Runnable(){
            public void run(){
                for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
                {

                    Long elapsedTime=(long) 0;
                    Long currentTime;



                    for(int innerLoop = 0; innerLoop < 10; innerLoop++){

                        long startTime = System.currentTimeMillis();
                        try {
                            int[] intArray = new int[outLoop];
                            sort.fillArray(intArray);
                            sort.quickSort(intArray);
                        } catch (InterruptedException e) {

                        }
                        long endTime = System.currentTimeMillis();
                        currentTime = endTime-startTime;
                        elapsedTime +=currentTime;
                        currentPercentMerge++;
                        repaint();
                    }

                    long finalTime = (long) (elapsedTime/10);

                    String[] current = new String[2];
                    current[0]=""+outLoop;
                    current[1]=""+finalTime;
                    quick.add(iQuick, current);

                    iQuick++;

                }
                allArrays.add(0, quick);
                quickBool = true;
                saveCheck();
                repaint();
            }
        }).start();

        new Thread(new Runnable(){
            public void run(){
                for(int outLoop = loopJump; outLoop <= loopFinish; outLoop+=loopJump)
                {

                    Long elapsedTime=(long) 0;
                    Long currentTime;



                    for(int innerLoop = 0; innerLoop < 10; innerLoop++){

                        long startTime = System.currentTimeMillis();
                        try {
                            int[] intArray = new int[outLoop];
                            sort.fillArray(intArray);
                            sort.mergeSort(intArray);
                        } catch (InterruptedException e) {
                        }
                        long endTime = System.currentTimeMillis();
                        currentTime = endTime-startTime;
                        elapsedTime +=currentTime;
                        currentPercentQuick++;
                        repaint();
                    }

                    long finalTime = (long) (elapsedTime/10);

                    String[] current = new String[2];
                    current[0]=""+outLoop;
                    current[1]=""+finalTime;
                    merge.add(iMerge, current);

                    iMerge++;

                }
                allArrays.add(1, merge);
                mergeBool = true;
                saveCheck();
                repaint();
            }
        }).start();
    }
}
它们使用来自另一个类的排序,我希望能够允许用户能够在运行时停止它们,而不使它们使用不同的排序方法。有什么想法吗

排序代码:

public class Sorts {
static Random arrayFill = new Random();
static final int GREATESTNUMBER = 100;

public void fillArray(int[] array){
    for(int i = 0; i < array.length; i++)
        array[i] = arrayFill.nextInt(GREATESTNUMBER);
}

/**
 * 
 * @param array
 * @throws InterruptedException
 */
public void bubbleSort(int[] array) throws InterruptedException {

    int n = array.length;

    for (int m = n; m >= 0; m--) {
        for (int i = 0; i < n - 1; i++) {
            int k = i + 1;
            if (array[i] > array[k]) {
                swap(i, k, array);
            }

        }
    }
}

/**
 * 
 * @param i
 * @param j
 * @param array
 */
private void swap(int i, int j, int[] array) {

    int temp;
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

/**
 * 
 * @param array
 * @throws InterruptedException
 */
public void selectionSort(int[] array) throws InterruptedException {

    for (int i = 0; i < array.length - 1; i++)
    {
        int index = i;
        for (int j = i + 1; j < array.length; j++)
            if (array[j] < array[index])
                index = j;

        int smallerNumber = array[index]; 
        array[index] = array[i];
        array[i] = smallerNumber;

        //delayThread();
    }
}

/**
 * 
 * @param a
 * @throws InterruptedException
 */
public void mergeSort(int[ ] a) throws InterruptedException
{
    int[] tmp = new int[a.length];
    mergeSort(a, tmp,  0,  a.length - 1);
}

/**
 * 
 * @param a
 * @param tmp
 * @param left
 * @param right
 * @throws InterruptedException
 */
private void mergeSort(int[ ] a, int[ ] tmp, int left, int right) throws InterruptedException
{
    if( left < right )
    {
        int center = (left + right) / 2;
        mergeSort(a, tmp, left, center);
        mergeSort(a, tmp, center + 1, right);
        merge(a, tmp, left, center + 1, right);
    }
}

/**
 * 
 * @param a
 * @param tmp
 * @param left
 * @param right
 * @param rightEnd
 * @throws InterruptedException
 */
private void merge(int[ ] a, int[ ] tmp, int left, int right, int rightEnd )throws InterruptedException
{
    int leftEnd = right - 1;
    int k = left;
    int num = rightEnd - left + 1;

    while(left <= leftEnd && right <= rightEnd)
        if(a[left]<=a[right])
            tmp[k++] = a[left++];
        else
            tmp[k++] = a[right++];

    while(left <= leftEnd)    // Copy rest of first half
        tmp[k++] = a[left++];

    while(right <= rightEnd)  // Copy rest of right half
        tmp[k++] = a[right++];

    // Copy tmp back
    for(int i = 0; i < num; i++, rightEnd--){
        a[rightEnd] = tmp[rightEnd];
        //delayThread();
    }

}

/**
 * 
 * @param array
 * @throws InterruptedException
 */
public void quickSort(int array[]) throws InterruptedException 
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
    quickSort(array, 0, array.length - 1);// quicksort all the elements in the array
}

/**
 * 
 * @param array
 * @param start
 * @param end
 * @throws InterruptedException
 */
public void quickSort(int[] arr, int low, int high) throws InterruptedException {

    if (arr == null || arr.length == 0)
        return;

    if (low >= high)
        return;

    //pick the pivot
    int middle = low + (high - low) / 2;
    int pivot = arr[middle];

    //make left < pivot and right > pivot
    int i = low, j = high;
    while (i <= j) {
        while (arr[i] < pivot) {
            i++;
        }

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

        if (i <= j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }

    //recursively sort two sub parts
    if (low < j)
        quickSort(arr, low, j);

    if (high > i)
        quickSort(arr, i, high);
}
}
每一种都有延迟

private void bubbleSortD(int[] array) throws InterruptedException 
{
    int n = array.length;

    for (int m = n; m >= 0; m--) {
        for (int i = 0; i < n - 1; i++) {
            int k = i + 1;
            if (array[i] > array[k]) {
                swapD(i, k, array);

            }
            delayThread();
        }

    }
} 
但是我找不到一种方法来做到这一点,让单曲和4像这个一样使用不同的排序,我不想把排序写3遍,如果有人有办法把它变成1,那就太神奇了。
顺便问一下,他们都在不同的班级。

你尝试了什么?我看不到与停止线程相关的代码。如果创建匿名线程,则无法停止它。@MuratK Not true;您仍然可以通过要求布尔值为true以使其继续,然后将该布尔值设置为false来停止它,允许线程优雅地终止,这实际上是我没有尝试过的,但删除了代码。我已经停止了匿名线程,不是一个非常干净的方式,但我想不出另一种方式。将其添加到问题的末尾如果你没有表现出尝试,人们会怀疑你没有努力找出如何做到这一点,这是问题的要求;表现出努力。您使用stopThread布尔值执行此操作的方式实际上是终止线程读取我的注释中的链接的正确方式;向下滚动到我该做什么
private void delayThread() throws InterruptedException
{
    repaint();
    Thread.sleep(slider.getValue());

    if(stopThread){
        startBut.setEnabled(true);
        stopBut.setEnabled(false);  
        stopThread = false;
        listBox.setEnabled(true);
        throw new RuntimeException();
    }
}
private void bubbleSortD(int[] array) throws InterruptedException 
{
    int n = array.length;

    for (int m = n; m >= 0; m--) {
        for (int i = 0; i < n - 1; i++) {
            int k = i + 1;
            if (array[i] > array[k]) {
                swapD(i, k, array);

            }
            delayThread();
        }

    }
}