GUI以并行方式检查大数字的原始值

时间:2014-11-24 20:38:50

标签: java multithreading primes biginteger

我的任务是创建一个启动线程的GUI,以便并行测试过大的数字。我想提供两个文本框,用户可以在其中键入数字。单击按钮时,我想为每个数字启动一个线程。线程完成后,我想将该计算的结果写入表单上的相应字段。我收到一些错误:

Exception in thread "Thread-2" java.lang.NullPointerException. as well as at primeBigIntegers.MyThread.IsPrime(MyThread.java:27) which is this line of code: 
if (num.mod(new BigInteger("2")).compareTo(BigInteger.ZERO) == 0) {
and another error at primeBigIntegers.MyThread.run(MyThread.java:43)

是:

 String output = num + " is" + (IsPrime(num) ? " " : " not "). 

所有显示框都显示正确,但代码未检查完好度。

 public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test09 frame = new Test09();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }});}
public Test09() {
    clickCounter = 0;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final JButton btnCalc = new JButton("Calculate");
    btnCalc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnCalc.setEnabled(false);{
                MyThread mt = new MyThread(lblNewLabel, clickCounter);
                mt.start();
            }
            btnCalc.setEnabled(true);
        }});
    btnCalc.setBounds(10, 239, 91, 23);
    contentPane.add(btnCalc);

    JLabel lblOfThreads = new JLabel("First Number: ");
    lblOfThreads.setHorizontalAlignment(SwingConstants.RIGHT);
    lblOfThreads.setBounds(10, 38, 110, 14);
    contentPane.add(lblOfThreads);

    txtNumberOfThreads = new JTextField();
    txtNumberOfThreads.setText("15485867");
    txtNumberOfThreads.setBounds(130, 35, 80, 20);
    contentPane.add(txtNumberOfThreads);
    txtNumberOfThreads.setColumns(10);

    JLabel lblOfThreads2 = new JLabel("Second Number: ");
    lblOfThreads2.setHorizontalAlignment(SwingConstants.RIGHT);
    lblOfThreads2.setBounds(10, 38, 110, 100);
    contentPane.add(lblOfThreads2);

    txtNumberOfThreads2 = new JTextField();
    txtNumberOfThreads2.setText("45862118");
    txtNumberOfThreads2.setBounds(130, 75, 80, 20);
    contentPane.add(txtNumberOfThreads2);
    txtNumberOfThreads2.setColumns(10);

    lblNewLabel = new JLabel("Results Here");
    lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
    lblNewLabel.setBackground(Color.BLACK);
    lblNewLabel.setBounds(10, 175, 206, 104);
    contentPane.add(lblNewLabel);
    }}

 public class MyThread extends Thread{

private JLabel myLabel;
private int threadNumber;
private BigInteger num; 

public MyThread(JLabel lblNewLabel, int i) {
    this.myLabel = myLabel;
    this.threadNumber = threadNumber;
    this.num= num; 
}

public static boolean IsPrime(BigInteger num) {
    if (num.mod(new BigInteger("2")).compareTo(BigInteger.ZERO) == 0) {
      return false;
    }
    for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(num) <= 0; i =
        i.add(new BigInteger("2"))) {
      if (num.mod(i).compareTo(BigInteger.ZERO) == 0) {
 return false;
      }}
    return true;
  }
public void run() {
    String output = num + " is" + (IsPrime(num) ? " " : " not ")
            + "a prime number.";
  JOptionPane.showMessageDialog(null, output);

}}

1 个答案:

答案 0 :(得分:3)

构造函数中的

this.num = num;不执行任何操作(没有参数num,因此您要为其分配this.num自身的值。 num仍然为空。你需要初始化它。