Javafx只启动一个线程,即使多次单击按钮也是如此

Javafx只启动一个线程,即使多次单击按钮也是如此,java,multithreading,javafx,Java,Multithreading,Javafx,我有一个按钮,它从字段中获取我的用户名和密码,并向后端发送身份验证请求。我正在使用线程,以便我的按钮动画和场景不会像这样冻结: Service<Void> service = new Service<Void>() { @Override protected Task<Void> createTask() { return new Task<Void>() {

我有一个按钮,它从字段中获取我的用户名和密码,并向后端发送身份验证请求。我正在使用线程,以便我的按钮动画和场景不会像这样冻结:

Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {
                        //do authentication
                        if(responseStatus != 200){
                            authenticated = false 
                        }else{
                            authenticate = true
                        }
                        Platform.runLater(() -> {
                        try{
                            if(authenticated) {
                                changeScene();
                            }
                        }finally{
                            latch.countDown();
                        }
                    });
                    latch.await();
                    return null;
                }
            };
        }
    };
    service.start();

private void changeScene(){
    try {
        Stage window = (Stage)loginPane.getScene().getWindow();
        LoggedFirstStyle.displayLoggedScene();
        window.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
服务=新服务(){
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的Void调用()引发异常{
//进行身份验证
如果(响应状态!=200){
已验证=错误
}否则{
验证=真
}
Platform.runLater(()->{
试一试{
如果(已验证){
改变场景();
}
}最后{
倒计时();
}
});
satch.wait();
返回null;
}
};
}
};
service.start();
私有void changeScene(){
试一试{
Stage window=(Stage)loginPane.getScene().getWindow();
LoggedFirstStyle.displayLoggedScene();
window.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
但问题是,如果我多次单击按钮,platform run later会执行多次,changeScene也会执行多次,并且会打开多个场景。我这样做可以吗?如果可以,我如何防止用同一方法打开多个线程?

A提供了“重用”A的功能。我把“重用”放在引号里,因为真正发生的是
服务
每次启动时都会创建一个新的
任务
。有关
服务
任务
之间的区别和用法的更多信息,您可以:

  • 阅读和的文档,或许也可以
  • 阅读
  • 请参阅此堆栈溢出问题:
由于
服务
是要重用的,因此您应该只创建一个实例。它还保持状态,因此只能“一次执行一次”。换句话说,同一个
服务
不能并行执行多次。当
服务
完成时,它将处于
成功
取消
失败
状态;要再次启动
服务
,您必须在再次调用
start()
之前调用
restart()
(将取消正在运行的
服务
)或调用
reset()

服务运行时,您可能希望禁用某些UI组件,以便用户无法多次尝试启动它。您可以通过侦听器和/或绑定来实现这一点。如果需要,您还可以签入,这样您的代码就不会试图启动
服务(如果它已经在运行)。是否需要这些检查取决于什么代码可以启动
服务
,以及如何执行

这里有一个小例子。它使用FXML创建接口,但重要的部分是
LoginController
LoginService
类。根据您的应用程序,您可能还需要添加取消登录的方法

Main.java

package com.example;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        // Login.fxml is in the same package as this class
        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Service Example");
        primaryStage.show();
    }

}
package com.example;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

public class LoginService extends Service<Boolean> {

    private final StringProperty username = new SimpleStringProperty(this, "username");
    public final void setUsername(String username) { this.username.set(username); }
    public final String getUsername() { return username.get(); }
    public final StringProperty usernameProperty() { return username; }

    private final StringProperty password = new SimpleStringProperty(this, "password");
    public final void setPassword(String password) { this.password.set(password); }
    public final String getPassword() { return password.get(); }
    public final StringProperty passwordProperty() { return password; }

    @Override
    protected Task<Boolean> createTask() {
        return new LoginTask(getUsername(), getPassword());
    }

    private static class LoginTask extends Task<Boolean> {

        private final String username;
        private final String password;

        public LoginTask(String username, String password) {
            this.username = username;
            this.password = password;
        }

        @Override
        protected Boolean call() throws Exception {
            Thread.sleep(3_000L); // simulate long running work...
            return !isCancelled() && "root".equals(username) && "root".equals(password);
        }

    }

}
package com.example;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Cursor;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class LoginController {

    @FXML private GridPane root;
    @FXML private TextField userField;
    @FXML private PasswordField passField;
    @FXML private Button loginBtn;

    private LoginService service;

    @FXML
    private void initialize() {
        service = new LoginService();
        service.usernameProperty().bind(userField.textProperty());
        service.passwordProperty().bind(passField.textProperty());

        // Don't let user interact with UI while trying to login
        BooleanBinding notReadyBinding = service.stateProperty().isNotEqualTo(Worker.State.READY);
        userField.disableProperty().bind(notReadyBinding);
        passField.disableProperty().bind(notReadyBinding);
        loginBtn.disableProperty().bind(notReadyBinding);

        root.cursorProperty().bind(
                Bindings.when(service.runningProperty())
                        .then(Cursor.WAIT)
                        .otherwise(Cursor.DEFAULT)
        );

        service.setOnSucceeded(event -> serviceSucceeded());
        service.setOnFailed(event -> serviceFailed());
    }

    private void serviceSucceeded() {
        if (service.getValue()) {
            /*
             * Normally you'd change the UI here to show whatever the user needed to
             * sign in to see. However, to allow experimentation with this example
             * project we simply show an Alert and call reset() on the LoginService.
             */
            showAlert(Alert.AlertType.INFORMATION, "Login Successful", "You've successfully logged in.");
            service.reset();
        } else {
            showAlert(Alert.AlertType.ERROR, "Login Failed", "Your username or password is incorrect.");
            service.reset();
        }
    }

    private void serviceFailed() {
        showAlert(Alert.AlertType.ERROR, "Login Failed", "Something when wrong while trying to log in.");
        service.getException().printStackTrace();
        service.reset();
    }

    private void showAlert(Alert.AlertType type, String header, String content) {
        Alert alert = new Alert(type);
        alert.initOwner(root.getScene().getWindow());
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }

    @FXML
    private void handleLogin(ActionEvent event) {
        event.consume();

        // isBlank() is a String method added in Java 11
        boolean blankUsername = userField.textProperty().getValueSafe().isBlank();
        boolean blankPassword = passField.textProperty().getValueSafe().isBlank();

        if (blankUsername || blankPassword) {
            showAlert(Alert.AlertType.ERROR, null, "Both username and password must be specified.");
        } else {
            service.start();
        }
    }

}
LoginService.java

package com.example;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        // Login.fxml is in the same package as this class
        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("Service Example");
        primaryStage.show();
    }

}
package com.example;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

public class LoginService extends Service<Boolean> {

    private final StringProperty username = new SimpleStringProperty(this, "username");
    public final void setUsername(String username) { this.username.set(username); }
    public final String getUsername() { return username.get(); }
    public final StringProperty usernameProperty() { return username; }

    private final StringProperty password = new SimpleStringProperty(this, "password");
    public final void setPassword(String password) { this.password.set(password); }
    public final String getPassword() { return password.get(); }
    public final StringProperty passwordProperty() { return password; }

    @Override
    protected Task<Boolean> createTask() {
        return new LoginTask(getUsername(), getPassword());
    }

    private static class LoginTask extends Task<Boolean> {

        private final String username;
        private final String password;

        public LoginTask(String username, String password) {
            this.username = username;
            this.password = password;
        }

        @Override
        protected Boolean call() throws Exception {
            Thread.sleep(3_000L); // simulate long running work...
            return !isCancelled() && "root".equals(username) && "root".equals(password);
        }

    }

}
package com.example;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Cursor;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class LoginController {

    @FXML private GridPane root;
    @FXML private TextField userField;
    @FXML private PasswordField passField;
    @FXML private Button loginBtn;

    private LoginService service;

    @FXML
    private void initialize() {
        service = new LoginService();
        service.usernameProperty().bind(userField.textProperty());
        service.passwordProperty().bind(passField.textProperty());

        // Don't let user interact with UI while trying to login
        BooleanBinding notReadyBinding = service.stateProperty().isNotEqualTo(Worker.State.READY);
        userField.disableProperty().bind(notReadyBinding);
        passField.disableProperty().bind(notReadyBinding);
        loginBtn.disableProperty().bind(notReadyBinding);

        root.cursorProperty().bind(
                Bindings.when(service.runningProperty())
                        .then(Cursor.WAIT)
                        .otherwise(Cursor.DEFAULT)
        );

        service.setOnSucceeded(event -> serviceSucceeded());
        service.setOnFailed(event -> serviceFailed());
    }

    private void serviceSucceeded() {
        if (service.getValue()) {
            /*
             * Normally you'd change the UI here to show whatever the user needed to
             * sign in to see. However, to allow experimentation with this example
             * project we simply show an Alert and call reset() on the LoginService.
             */
            showAlert(Alert.AlertType.INFORMATION, "Login Successful", "You've successfully logged in.");
            service.reset();
        } else {
            showAlert(Alert.AlertType.ERROR, "Login Failed", "Your username or password is incorrect.");
            service.reset();
        }
    }

    private void serviceFailed() {
        showAlert(Alert.AlertType.ERROR, "Login Failed", "Something when wrong while trying to log in.");
        service.getException().printStackTrace();
        service.reset();
    }

    private void showAlert(Alert.AlertType type, String header, String content) {
        Alert alert = new Alert(type);
        alert.initOwner(root.getScene().getWindow());
        alert.setHeaderText(header);
        alert.setContentText(content);
        alert.showAndWait();
    }

    @FXML
    private void handleLogin(ActionEvent event) {
        event.consume();

        // isBlank() is a String method added in Java 11
        boolean blankUsername = userField.textProperty().getValueSafe().isBlank();
        boolean blankPassword = passField.textProperty().getValueSafe().isBlank();

        if (blankUsername || blankPassword) {
            showAlert(Alert.AlertType.ERROR, null, "Both username and password must be specified.");
        } else {
            service.start();
        }
    }

}
Login.fxml



请说明您如何在应用程序的更大范围内使用
服务。看起来您每次都在创建一个新实例,这不是正确的用法。也就是说,解决方案是在
服务运行时(或在第一次单击之后)禁用
按钮,因为这样可以防止用户再次启动按钮。@Slaw正确用法是什么?您的意思是我应该创建一个扩展服务的新类并将其初始化为全局变量吗?我的意思是,我可以在全局变量中使用匿名类,但它看起来有点糟糕。此外,当我这样做时,它抛出IllegalStateException:“只能在就绪状态下启动服务。处于运行状态”我是否应该捕获它?@Slaw我还可以同步登录事件侦听器的body方法,并对已验证的进行if语句,并说只有在尚未验证的情况下才能进入,但我觉得这样做是不对的。那么,扩展抽象服务并使用同一实例是最好的方法吗?