- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 TextField 的简单 JavaFX 阶段。我想做的是:当用户将字母插入文本字段时,我想打印“现在”(只是为了看看它是否有效)。我使用线程是因为稍后我想扫描字典以查看用户输入的字母是否是字典中单词的一部分。
但我得到:java.lang.IllegalMonitorStateException
有什么想法吗?我似乎不理解 Condition.await 和多线程的整个概念..
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 工具包的基本功能之一)。要响应文本字段中文本的更改,您所需要做的就是使用文本字段的文本属性注册更改监听器:
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
class提供您所需的功能:
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();
}
关于java - 线程通信: How to signal that a key was clicked? java.lang.IllegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256552/
我是 Java 新手(也是 RoR 开发人员)。 我有一个简单的程序。球由球员共享。球应传给随机玩家。 好的,代码如下: class Ball { private int currentPl
我是否没有正确使用同步: 在下面的代码中我遇到了两个问题: 1.在将方法(designBusiness、createBusiness、sellBusiness)设置为 synchronized 时,就
我不完全理解wait和notify(Object)是如何工作的,结果我被迫瘦身将我的尝试记入以下代码部分。 Main.java: import java.util.ArrayList; class M
我有这个代码 if( id == 0 ||(id % 2) != 0){ System.out.println("test"); synchronized(lo
我是 Java 新手。我刚刚读了《java核心》这本书。我遇到了有关“条件和锁定”的问题。 我从书上敲了一段代码到eclipse中做了一些练习。 当我运行代码时,“sufficientFund.wai
我想模拟一个妈妈叫三个 child 吃饭,这是我的代码: class child extends Thread{ String name; mother mom; public
我正在尝试在新线程中调用方法,该方法将在使用 callable 延迟后返回一些东西。由于,它正在下降IllegalMonitorStateException 当我只创建类的实例,调用方法并且该方法将延
我已经使用 java 线程构建了生产者-消费者。生产者和消费者是两个不同的类,指的是单链表和对象锁。下面的实现有什么问题? Item Produced by Thread-0 Item 1 Item
我在我的代码中遇到了 IllegalMonitorStateException,我不确定为什么会收到它以及如何修复它。我当前的代码是并且错误发生在 try block 中: public stat
请向我解释为什么我的代码在等待函数中抛出 IllegalMonitorStateException ,据我所知,只有在同步部分未完成时才会发生这种情况? private void deliver(in
每当我调用pauseThread()时,它总是抛出IllegalMonitorStateException。 我在文档中注意到我需要拥有对象监视器才能使线程等待。 用这段代码 synchronized
我正在尝试学习java并发编程。请检查我的示例代码并帮助我理解为什么我会收到“java.lang.IllegalMonitorStateException”,即使我在同步上下文中调用了 wait()
我对监视器和条件变量不熟悉。我在监视器中使用锁定和条件变量。 public class Monitor { private final int piNumberOfPhilosopher
所以我正在尝试编写一个程序来打印出以下输出: 44 33 22 11 该程序应该是多线程的,并且必须使用锁来防止竞争条件。它还必须利用 Condition,以便当线程想要打印的数字与变量 thread
我对多线程了解甚少。 我期望下面的程序能够完美运行,但它不起作用并产生以下异常。 Exception in thread "Thread-1" java.lang.IllegalMonitorStat
我不完全理解wait和notify(Object的)的工作方式,因此,我不得不将尝试减少到以下代码部分。 Main.java: import java.util.ArrayList; class
void waitForSignal(){ Object ob =new Object(); synchronized (Thred.currentThread()) {
我收到一个 IllegalMonitorStateException,我觉得我无法阅读更多内容,所以我问是否有人知道我做错了什么。在计数器(门类中线程之间的共享资源)陷入等待状态后,我在程序运行结束时
我有一个程序可以模拟一艘船的大门。它们在线程中运行。这个想法是让它们在 run 方法中的随机时刻运行和暂停,以模拟路过的人。这是由所有线程完成的,同时主线程正在等待通知并检查船是否已满,当线程通知他们
我想在线程中运行的 Vector 中使用同步 我尝试使用两种方法,一种用于等待,另一种用于通知 我想从静态类调用notify方法这是我的线程代码: import java.io.IOException
我是一名优秀的程序员,十分优秀!