I have a Java FX 2 app which is updating hundreds of times a second. I'm getting a problem where the labels are partially blanked for a fraction of a second but this happens fairly often. How do I fix this?
问题:
回答1:
Calling Platform.runLater() hundreds of times a second is not a good idea. I advise you throttle the input speed of your data source or batch your data together before invoking Platform.runLater() to update your UI, such that Platform.runLater isn't invoked > 30 times a second.
I filed a jira request RT-21569 to improve the documentation on the Platform.runLater call and consider implementing a superior runLater event throttling system in the underlying platform.
A sample solution for batching runLater events is given by Richard Bair in this 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);
}
}
Comments by Richard Bair copied from the referred forum thread:
Here is an example that simulates some long running thread producing copious amounts of data. I found that it wasn't working quite to my liking. If the Thread.sleep is missing, it still swamps the process (perhaps it is the way I'm handling the concurrency in this case). I also found if I reduced it to "2" ms of sleeping then I couldn't grab the scroll bar thumb and move it around. I think the problem here is that there is a mouse event in the event queue for the press and drag, but between drag events new items are added to the list causing the thumb to move and since this happens more frequently than my drag events the thumb never goes where I want it to be. This I think is due to the way the thumb position is handled based on the number of rows, not sure what can be done about it except for the application to throttle and batch up the rows being added as I do here.