0

我一直在编写一个程序,它可以搜索一个数字列表,以找到与某个其他数字相加的那些。那里没有问题,算法虽然可能不是很有效,但很实用。

现在必须从文本文件中获取数字列表,但我一直在尝试这样做,以便用户可以将列表复制粘贴到 TextArea 中,按 Enter,然后让程序将字符串发送回普通(非 GUI)线程。

为此,我遵循了这个例子(最佳答案)。我使用的是按键事件而不是按钮按下,以及字符串而不是链接列表,但除此之外,非常相似。

我创建和运行 TextDemo 的代码(是的,我改编了一个教程程序):

  /*Copy paste text in window */
  public static String copypaste() throws Exception{
    String text = "";
    final TextDemo demo = new TextDemo();
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        demo.createAndShowGUI();
      }
    });
    synchronized(demo.text){
      while(demo.text.equals("")){   //if the window is unused
        demo.text.wait();
      }
      text = demo.text;
    }
    return text;
  }

TextDemo 本身(减去免责声明,请不要提醒 Oracle :)):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextDemo extends JPanel implements KeyListener{
    protected JTextArea textArea;
    private final static String newline = "\n";
    public String text = "";
    boolean used = false;

    public TextDemo() {
        super(new GridBagLayout());

        textArea = new JTextArea(100, 30);
        textArea.addKeyListener(this);

        textArea.setEditable(true);
        JScrollPane scrollPane = new JScrollPane(textArea);

        //Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
    }

    public void keyPressed(KeyEvent e) {
        // Listen for the key pressed and check it against "Enter"
        // Then read out of our textarea control and print to screen4       
        if (e.getKeyCode() == e.VK_ENTER) {
          synchronized(text){
            text = textArea.getText();
            System.out.println("Text entered.");
            text.notify();
          }
            }
    }

    public void keyReleased(KeyEvent e) {
        // Listen for the key pressed and check it against "Enter"
        // Then read out of our textarea control and print to screen4       
        if (e.getKeyCode() == e.VK_ENTER) {
            //do nothing
        }
    }

    public void keyTyped(KeyEvent e) {
        // Listen for the key pressed and check it against "Enter"
        // Then read out of our textarea control and print to screen4       
        if (e.getKeyCode() == e.VK_ENTER) {
            //do nothing
        }
    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add contents to the window.
        frame.add(new TextDemo());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

当我运行代码时,它似乎可以工作,直到我按下回车键并且我的程序崩溃了。错误代码(我只包括前 5 行,完整版在这里:http: //img.photobucket.com/albums/v242/ChaosGuide/illegalmonitorstateexception.png):

Exception in thread "AWT-EventQue-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at TextDemo.keyPressed(TextDemo.java:72)
    at java.awt.Component.processKeyEvent(Component.java:6463)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
    at java.awt.Component.processEvent(Component.java:6282)

这是我第一次做任何涉及线程的事情,所以我真的不明白我做错了什么。

任何帮助是极大的赞赏。

4

1 回答 1

1

两个线程都使用demo.text它们的锁,并在这个对象上调用wait()notify()进行通信,在一个同步块内demo.text,这是正确的。但是在调用notify(). 因此,实际上,您调用notify()了一个您不拥有锁的对象:

synchronized(text) { // this block is synchronized on the empty string object
    text = textArea.getText(); // you assign another string to text
    System.out.println("Text entered.");
    text.notify(); // you call notify on this other string object, but you don't own its lock, because you synchronized on the empty string
}

经验法则:当一个变量用作锁时,它应该是最终的,以避免这种错误。此外,使用空字符串作为锁是一个非常非常糟糕的主意。你最好创建一个专用对象来做到这一点:

private final Object lock = new Object();

但是最好的办法是忘记wait()and notify(),它们太低级了,并使用 java.util.concurrent 包中的更高级别的抽象,例如信号量。

甚至更好:与其让主线程等待事件调度线程,不如启动一个后台线程,或者使用 SwingWorker 来执行您的冗长操作。

于 2013-05-09T12:06:55.913 回答