我一直在编写一个程序,用于搜索数字列表以查找与某个其他数字相加的数字。没有问题,该算法虽然可能不是很有效,但功能齐全。
现在必须从文本文件中获取数字列表,但我一直在尝试这样做,以便用户可以将列表复制粘贴到 TextArea 中,按 Enter 键,然后让程序发送返回到普通(非 GUI)线程的字符串。
为此,我关注了 this example (最上面的答案)。我使用按键事件而不是按下按钮,使用字符串而不是链接列表,但除此之外,非常相似。
我创建并运行 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)
这是我第一次做任何涉及线程的事情,所以我真的不明白我做错了什么。
非常感谢任何帮助。
最佳答案
两个线程都使用 demo.text
作为锁,并在该对象上调用 wait()
和 notify()
进行通信。 demo.text
上的同步块(synchronized block),这是正确的。但您要在调用 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
}
经验法则:当变量用作锁时,它应该是final的,以避免这种错误。此外,使用空字符串作为锁是一个非常非常糟糕的主意。您最好创建一个专用对象来执行此操作:
private final Object lock = new Object();
但是最好的办法是忘记 wait()
和 notify()
,它们太低级了,而使用更高级别的从 java.util.concurrent 包中抽象出来,例如信号量。
或者甚至更好:您最好启动一个后台线程,或者更确切地说使用 SwingWorker 来执行冗长的操作,而不是让主线程等待事件调度线程。
关于java - 无法从 GUI 线程获取字符串到 java 中的 'logic' 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460978/