【发布时间】:2019-04-15 23:29:52
【问题描述】:
我是线程的新手,我对它们了解不多,但是我主要是在努力创建它们。
我创建了一个 GUI 程序,它使用选择排序、插入排序或合并排序对随机生成的整数数组进行重复排序。每次排序都对一个列表进行操作,该列表的大小是 2 的幂。每次排序完成后,将显示已排序的元素数和所用的毫秒数。
我已经完成了 3 个类,它们是合并、选择和插入。
我的合并排序工作正常,但是在选择和插入时我仍然遇到问题。我不确定我的“if”和“else”语句是否不正确,或者线程本身是否错误,但我正在努力解决它们。
这是我在主课上的内容。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Sorter extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
public static final String SELECTION_SORT_TEXT = "Selection Sort";
public static final String INSERTION_SORT_TEXT = "Insertion Sort";
public static final String MERGE_SORT_TEXT = "Merge Sort";
private JComboBox sortingAlgorithms;
private JTextArea display;
private JButton sortButton;
private JPanel panel;
private JLabel loadingIcon;
private JLabel sort;
private String[] options = {SELECTION_SORT_TEXT, INSERTION_SORT_TEXT, MERGE_SORT_TEXT};
public Sorter()
{
setTitle("Sorter");
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}
private void createContents()
{
//TODO: Implement
this.panel = new JPanel(new FlowLayout());
this.display = new JTextArea();
this.loadingIcon = new JLabel(new ImageIcon("loading.gif"));
this.sort = new JLabel("Sorting Algorithm");
this.sortButton = new JButton("Sort");
this.sortingAlgorithms = new JComboBox<>(options);
loadingIcon.setSize(25,25);
display.setBorder(new EmptyBorder(10,10,10,10));
panel.add(sort);
panel.add(sortingAlgorithms);
panel.add(sortButton);
panel.add(loadingIcon);
sortButton.addActionListener(new SortButtonListener());
loadingIcon.setVisible(false);
display.setEnabled(false);
setLayout(new BorderLayout());
add(panel, BorderLayout.NORTH);
add(display, BorderLayout.CENTER);
}
private class SortButtonListener implements ActionListener
{
private int[] arr;
private SortRunnable sr;
public void actionPerformed(ActionEvent e)
{
sr.run();
ExecutorService es = Executors.newSingleThreadExecutor();
//TODO: Finish Implementation
if(e.getSource() == sortButton)
{
sortingAlgorithms.setEnabled(false);
sortButton.setEnabled(false);
loadingIcon.setVisible(true);
display.setText("N\t\tRuntime (ms)");
}
arr = new int [2000];
for(int i = 0; i <= 8; i++)
{
arr = new int [(int) Math.pow(2, i) * 1000];
fillArr();
sr = new SortRunnable((String) sortingAlgorithms.getSelectedItem(), arr, Sorter.this);
es.execute(sr);
}
Thread sortContext = new Thread(new SortRunnable((String)
sortingAlgorithms.getSelectedItem(), arr, Sorter.this));
sortContext.start();
es.shutdown();
}
/*
These values are powers of 2 from 0 to 8, times 1000.
*/
private void fillArr()
{
Random r = new Random();
int n = 0;
for(int i=0; i<arr.length; ++i)
{
arr[i] = r.nextInt();
}
}
}
/*
The displayResult method is responsible for adding the provided sort runtime information to
the display. It should also check to see if the final sort runtime information is present. If so,
it should hide the loading gif and enable the JComboBox and sort button.
*/
public synchronized void displayResult(int n, long runtime)
{
//TODO: Implement
display.append("\n" + n + "\t\t" + runtime);
}
public static void main(String[] args)
{
new Sorter();
}
}
我不太关心上面的内容。我更关心如何在创建这些线程的 SortRunnable 类中创建我的线程。这是对这门课的期望。
此类包含用于计时和执行所选排序的代码。
• 您可以在排序的开始和结束时找到对 System.currentTimeMillis() 的调用之间的区别,以获取其运行时间。
• 在执行搜索之前调用 Runnable 中的“Thread.yield()”,以确保 GUI 线程具有根据需要更新其显示所需的优先级。
• 排序完成后,应在 GUI 线程中更新显示。这可以通过在 Sorter 引用上调用“displayResult”方法来完成。此调用应发生在 Runnable 对象的 run 方法中,作为参数传递给 SwingUtilities invokeLater 方法,如下所示:
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
//call Sorter's displayResult method here
}
});
这是我需要帮助的代码。
import javax.swing.*;
public class SortRunnable implements Runnable
{
private String sortingAlgorithm;
private int[] arr;
private Sorter sorter;
public SortRunnable(String sortingAlgorithm, int[] arr, Sorter sorter)
{
this.sortingAlgorithm = sortingAlgorithm;
this.arr = arr;
this.sorter = sorter;
}
@Override
public void run()
{
Thread.yield();
if(sortingAlgorithm.equals(sorter.MERGE_SORT_TEXT))
{
MergeSort.mergeSort(arr);
}
Thread.yield();
if(sortingAlgorithm.equals(sorter.SELECTION_SORT_TEXT))
{
SelectionSort.sort(arr);
}
Thread.yield();
if(sortingAlgorithm.equals(sorter.INSERTION_SORT_TEXT))
{
InsertionSort.sort(arr);
}
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
Thread.yield();
sorter.displayResult(arr.length, System.currentTimeMillis());
//call Sorter's displayResult method here
}
});
}
}
我只需要插入和选择线程的帮助。谢谢!如果您想要他们的个人课程,以便您可以看到其中的内容,请告诉我。
【问题讨论】:
-
糟糕的标题。重写以总结您的具体技术问题。
-
@BasilBourque 我这里没有放三个类,但它们是 MergeSort、InsertionSort 和 SelectionSort。他们做所有的排序。是的,我如何将它们分别作为线程。
-
您的标题需要重写以显示此问题与29 million posts on Stack Overflow related to Java & threads 有何不同。
标签: java user-interface executorservice execution java-threads