[英]Exception in thread “main” java.lang.IllegalMonitorStateException
[英]Thread communication: How to signal that a key was clicked? java.lang.IllegalMonitorStateException
我有一个带有TextField的简单JavaFX阶段。 我想做的是:当用户在TextField中插入字母时,我想打印“现在”(只是看它是否有效)。 我使用线程是因为稍后我想扫描字典,以查看用户输入的字母是否是词典中单词的一部分。
但是我得到了:java.lang.IllegalMonitorStateException
有任何想法吗? 我似乎不太了解Condition.await和Multithreading的整个概念。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class DictionaryThreading extends Application {
private static Lock lock = new ReentrantLock();
public static Condition condition = lock.newCondition();
public static void main(String[] args) {
launch();
}
private static class ScanWords implements Runnable{
@Override
public void run() {
lock.lock();
try{
while(true){
this.wait();
System.out.println("clicked");
}
} catch (Exception e){
e.printStackTrace();
} finally{
lock.unlock();
}
}
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
new ScanWords().run();
TextField tf = new TextField("Please enter a word");
tf.setOnKeyPressed(e -> {});
pane.getChildren().add(tf);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
无需创建一个除了等待用户事件外不执行其他操作的线程。 JavaFX框架已经为您提供了此功能(它是任何 UI工具箱的基本功能之一)。 要响应文本字段中的文本更改,您需要做的就是在文本字段的text属性中注册一个更改侦听器:
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
TextField tf = new TextField("Please enter a word");
tf.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("text changed");
});
pane.getChildren().add(tf);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
如果需要花费很长时间来响应文本更改,那么您应该在文本字段的侦听器中的后台线程中启动该过程。 如果要搜索的内容很大,则可能要取消任何现有的搜索,以免最终并没有同时运行大量搜索。 JavaFX Service
类提供了为此所需的功能:
public class SearchService extends Service<List<String>> {
// modify and access only on FX Application Thread:
private String searchString ;
@Override
protected Task<List<String>> createTask() {
final String s = searchString ;
return new Task<List<String>>() {
@Override
protected List<String> call() throws Exception {
List<String> matches = new ArrayList<>();
// do search for strings matching s
// be sure to check isCancelled() regularly
return matches ;
}
};
}
public String getSearchString() {
checkThread();
return searchString ;
}
public void setSearchString(String searchString) {
checkThread();
this.searchString = searchString ;
}
private void checkThread() {
if (! Platform.isFxApplicationThread()) {
throw new IllegalStateException("Not on FX Application Thread");
}
}
}
那你可以做
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
SearchService searchService = new SearchService();
searchService.setOnSucceeded(e -> {
List<String> matches = searchService.getValue();
// do whatever you need with search results...
// this is called on FX application thread
});
TextField tf = new TextField("Please enter a word");
tf.textProperty().addListener((obs, oldText, newText) -> {
searchService.cancel();
searchService.setSearchText(newText);
searchService.restart();
});
pane.getChildren().add(tf);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
我不使用JavaFX,但我认为您需要使用EventListener
。 尝试使用TextListener
或InputMethodListener
。 例如:
StackPane pane = new StackPane();
TextField tf = new TextField("Please enter a word");
tf.addTextListener(e -> System.out.println("Pushed"));
pane.getChildren().add(tf);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
wait
方法应在synchronized
块内执行:
try{
while(true){
synchronized(this){
this.wait();
System.out.println("clicked");
}
}
} catch (Exception e){
e.printStackTrace();
} finally{
lock.unlock();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.