JavaFx和并发

时间:2015-07-30 10:01:36

标签: multithreading concurrency javafx

我已获得以下代码:

我项目的Controller.class:

    private Task<ObservableList<Payment>> allEntrieTask= new Task<ObservableList<Payment>>(){
        @SuppressWarnings("unchecked")
        @Override
        protected ObservableList<Payment> call() throws Exception {
            List<Payment> list = (List<Payment>) sqlExecutor.listAllEntries(Payment.class);
            ObservableList<Payment> payments = (ObservableList<Payment>) FXCollections.observableList(list);
            FXCollections.sort(payments);
            return payments;
        }

    };

    private Task<ObservableList<Payment>> revenueEntrieTask= new Task<ObservableList<Payment>>(){
        @SuppressWarnings("unchecked")
        @Override
        protected ObservableList<Payment> call() throws Exception {
            List<Payment> list = (List<Payment>) sqlExecutor.listEntries(Payment.class, Restrictions.gt("payment", 0.0));
            ObservableList<Payment> payments = FXCollections.observableList(list);
            FXCollections.sort(payments);
            return payments;
        }

    };

    private Task<ObservableList<Payment>> outputsEntrieTask= new Task<ObservableList<Payment>>(){
        @SuppressWarnings("unchecked")
        @Override
        protected ObservableList<Payment> call() throws Exception {
            List<Payment> list = (List<Payment>) sqlExecutor.listEntries(Payment.class, Restrictions.lt("payment", 0.0));
            ObservableList<Payment> payments = FXCollections.observableList(list);
            FXCollections.sort(payments);
            return payments;
        }

    };
public void onTreeAction(MouseEvent e){
        if(e.getClickCount() == 2){
            String chosenItem = tree.getSelectionModel().getSelectedItem().getValue(); //Ausgewähltes Item
            if(chosenItem.equals("Alle Zahlungen")){
                listAllPayments();
                actualTable = "Alle Zahlungen";
            }else if(chosenItem.equals("Einzahlungen")){
                listAllRevenues();
                actualTable = "Einzahlungen";
            }else if(chosenItem.equals("Auszahlungen")){
                listAllOutputs();
                actualTable = "Auszahlungen";
            }
        }
    }

    private void listAllPayments(){
        Thread t = new Thread(allEntrieTask);
        t.setDaemon(true);
        t.start();
        allEntrieTask.setOnSucceeded(event -> table.setItems(allEntrieTask.getValue()));
    }

    private void listAllRevenues(){
        Thread t = new Thread(revenueEntrieTask);
        t.setDaemon(true);
        t.start();
        revenueEntrieTask.setOnSucceeded(event -> table.setItems(revenueEntrieTask.getValue()));
    }

    private void listAllOutputs(){
        Thread t = new Thread(outputsEntrieTask);
        t.setDaemon(true);
        t.start();
        outputsEntrieTask.setOnSucceeded(event -> table.setItems(outputsEntrieTask.getValue()));
    }

这是我在java中使用并发时的样子吗?我已经推出了所有可能需要一些时间来执行的代码,在指定的Item开始的任务中在TreeView中被选中。我得到的另一个问题是,我得到了这段代码:

   private void listAllPayments(){
    Thread t = new Thread(allEntrieTask);
    t.setDaemon(true);
    t.start();
    allEntrieTask.setOnSucceeded(event -> table.setItems(allEntrieTask.getValue()));
}

此操作:allEntrieTask.setOnSucceeded(event -> table.setItems(allEntrieTask.getValue()));正在FXApplicationThread上运行吗?或者我必须做一些像Platform.runLater(//do some stuff)这样的事情。我的项目工作正常,没有任何错误或信息

0 个答案:

没有答案