我有一个Java FX 2应用程序,每秒更新数百次。 我遇到一个问题,标签部分消隐了几分之一秒,但这种情况经常发生。 我该如何解决这个问题?
答案 0 :(得分:1)
每秒拨打Platform.runLater()数百次并不是一个好主意。我建议您在调用Platform.runLater()以更新UI之前限制数据源的输入速度或批量处理数据,这样就不会调用Platform.runLater>每秒30次。
我提交了一个jira请求RT-21569来改进有关Platform.runLater调用的文档,并考虑在底层平台中实现一个优秀的runLater事件限制系统。
用于批处理runLater事件的示例解决方案由Richard Bair在此forum thread中提供。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;
/**
*
*/
public class BackgroundLoadingApp extends Application {
private ListView<String> listView;
private List<String> pendingItems;
private Semaphore lock = new Semaphore(1);
protected void addItem(String item) throws InterruptedException {
if (Platform.isFxApplicationThread()) {
listView.getItems().add(item);
} else {
// It might be that the background thread
// will update this title quite frequently, and we need
// to throttle the updates so as not to completely clobber
// the event dispatching system.
lock.acquire();
if (pendingItems == null) {
pendingItems = new LinkedList<String>();
pendingItems.add(item);
Platform.runLater(new Runnable() {
@Override public void run() {
try {
lock.acquire();
listView.getItems().addAll(pendingItems);
pendingItems = null;
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.release();
}
}
});
} else {
pendingItems.add(item);
}
lock.release();
}
}
/**
* The main entry point for all JavaFX applications.
* The start method is called after the init method has returned,
* and after the system is ready for the application to begin running.
* <p/>
* <p>
* NOTE: This method is called on the JavaFX Application Thread.
* </p>
*
* @param primaryStage the primary stage for this application, onto which
* the application scene can be set. The primary stage will be embedded in
* the browser if the application was launched as an applet.
* Applications may create other stages, if needed, but they will not be
* primary stages and will not be embedded in the browser.
*/
@Override public void start(Stage primaryStage) throws Exception {
listView = new ListView<String>();
primaryStage.setScene(new Scene(listView));
primaryStage.show();
// Start some background thread to load millions of rows as fast as it can. But do
// so responsibly so as not to throttle the event thread
Thread th = new Thread() {
@Override public void run() {
try {
for (int i=0; i<2000000; i++) {
addItem("Item " + i);
if (i % 200 == 0) {
Thread.sleep(20);
}
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
th.setDaemon(true);
th.start();
}
public static void main(String[] args) {
launch(args);
}
}
Richard Bair的评论复制自推荐的论坛帖子:
这是一个模拟一些长时间运行的线程产生大量数据的例子。 我发现它并不是我喜欢的。如果Thread.sleep丢失,它仍然会淹没进程(也许这就是我在这种情况下处理并发的方式)。我还发现,如果我把它减少到“2”ms的睡眠状态,那么我就无法抓住滚动条拇指并移动它。我认为这里的问题是在事件队列中有按下和拖动的鼠标事件,但在拖动事件之间新项目被添加到列表中导致拇指移动,因为这比我的拖动事件更频繁地发生拇指永远不会去我想要的地方。我认为这是由于根据行数处理拇指位置的方式,不知道可以做些什么,除了应用程序来限制和批量添加的行,就像我在这里一样。