JFX TableView(概念証明アプリケーションの証明)でセルを頻繁に更新しようとしました。 FXMLを介してTableViewをロードし、ExecutorServiceを起動してセルの値を変更します。JavaFX TableViewの頻繁な更新
私がアプリケーションを起動すると、最初の300万から400万の要素に対してアップデートが機能していることがわかりました。アップデートを遅くすると(MAGIC#1参照)動作します(10msはまだ速いですが、100msの遅延が働きます)。だから私はそれがスレッドの問題かもしれないと思った。
しかし、空のChangeListener(MAGIC#2参照)をプロパティに追加すると、それが正常に動作することがわかりました。マジック#1の必要がなくても。
何か間違っていますか?私は別の方法で細胞を更新する必要がありますか?
ご協力いただきありがとうございます。
テーブルビュー内の要素:
public class Element {
public static final AtomicInteger x = new AtomicInteger(0);
private final StringProperty nameProperty = new SimpleStringProperty("INIT");
public Element() {
// MAGIC#2
// this.nameProperty.addListener((observable, oldValue, newValue) -> {});
}
public void tick() {
this.setName(String.valueOf(x.incrementAndGet()));
}
public String getName() ...
public void setName(String name)...
public StringProperty nameProperty() ...
}
FXML用コントローラ:
public class TablePerformanceController implements Initializable {
private final ObservableList<Element> data = FXCollections.observableArrayList();
public Runnable changeValues =() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) break;
data.get(0).tick();
// MAGIC#1
// try { Thread.sleep(100); } catch (Exception e) {}
}
};
private ExecutorService executor = null;
@FXML
public TableView<Element> table;
@Override
public void initialize(URL location, ResourceBundle resources) {
this.table.setEditable(true);
TableColumn<Element, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(cell -> cell.getValue().nameProperty());
this.table.getColumns().addAll(nameCol);
this.data.add(new Element());
this.table.setItems(this.data);
this.executor = Executors.newSingleThreadExecutor();
this.executor.submit(this.changeValues);
}
}
どうもありがとう:
ここであなたが示したコントローラのコードと基本的に同等であるスタンドアロンのテストは、です。これは私の問題を解決します:-) –