java - javafx 应用程序的性能问题

标签 java javafx javafx-2

我正在使用 javafx 构建桌面应用程序,我正在使用 ftp 下载大约 500 MB 的文件。我正在使用 DirectoryChooser 选择下载位置,但选择目录后我的应用程序挂起并且没有响应。 虽然文件已下载。

这是我的代码:-

try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            success = ftpClient.changeWorkingDirectory(PATH + preset + "/" +  file_to_download + offset);
            System.out.println("Download Path:-" + PATH + preset + "/" +  file_to_download + offset);
            if (!success) {
                System.out.println("Could not changed the directory to RIBS");
                return;
            } else {
                System.out.println("Directory changed to RIBS");
            }
            FTPFile[] files = ftpClient.listFiles();
            for (FTPFile file : files) {
                if (file.getName().contains(".zip")) {
                    dfile = file.getName();
                }

            }
            DirectoryChooser dirChooser = new DirectoryChooser();
            File chosenDir = dirChooser.showDialog(tableView.getScene().getWindow());
            System.out.println(chosenDir.getAbsolutePath());
            OutputStream output;
            output = new FileOutputStream(chosenDir.getAbsolutePath() + "/" + dfile);
            int timeOut = 500;
            ftpClient.setConnectTimeout(timeOut);
            if (ftpClient.retrieveFile(dfile, output) == true) {
                downloadButton.setDisable(true);
            }
            output.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

我该如何改进?

最佳答案

您正在应用程序线程上执行下载,这会阻止 UI。查看有关 JavaFX 并发性的文档。 https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm

关于java - javafx 应用程序的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389881/

相关文章:

java - 如何在 Set 中添加重复元素?

java - 使用斐波那契堆改进 Dijkstra?

java - 从 OpenCV 的 LBP 实现中提取特征向量

javafx - Gluon JavaFX 16 EA 4 上的 "getNativeWindowHandle+0x54"通过 DRM

异常 : (The process can not access the file because this file is used by another process)

java - Scene.getStylesheets().add() 在 jar 文件中不起作用

java - 什么是抽象类和抽象方法?

每个选定项目的 JavaFX ComboBox 背景颜色

java - JavaFX 中的可编辑 TableView,仅输入数字

java - 使用 JavaFX 部署打包外部文件