gpt4 book ai didi

java - 如何从 2 个独立的 JavaFX 服务线程中获取结果?

转载 作者:行者123 更新时间:2023-11-29 04:43:05 25 4
gpt4 key购买 nike

我是 javaFx 的新手,所以我可能在这里遗漏了一些简单的东西,但我似乎找不到这个解决方案。我有一个应用程序需要并行执行两个单独的函数,然后仅当两个函数都返回了返回值时才需要更新 UI。我已经创建了一些测试代码并在下面看到,除了结果之外,一切正常。我怎样才能得到它来打印线程执行的结果。在我的示例中,标签应显示为 thread1Result: false thread2Result: true 但两者都显示为 null。

主类

public class Main extends Application {
public static void main( String[] args ) {
launch( args );
}

/* (non-Javadoc)
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start( Stage primaryStage ) throws Exception {
Parent root = FXMLLoader.load( getClass().getResource( "/application/LoadPage.fxml" ) );
Scene scene = new Scene( root );
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();

}
}

加载页面.fxml

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">

Controller 类

public class Controller {
@FXML Label lbl_Status;
Boolean thread1Result = null;
Boolean thread2Result = null;

public void doIt(ActionEvent event){
Button doitButton = ((Button) event.getSource() );
doitButton.setDisable( true );
ExecutorService es = Executors.newFixedThreadPool(2);
final ServiceExample service1 = new ServiceExample(4000);
final ServiceExample service2 = new ServiceExample(5000);
service1.setExecutor( es );
service1.setOnSucceeded( new EventHandler<WorkerStateEvent>() {
@Override
public void handle( WorkerStateEvent arg0 ) {
thread1Result = service1.getValue();
}
});
service2.setExecutor( es );
service2.setOnSucceeded( new EventHandler<WorkerStateEvent>() {
@Override
public void handle( WorkerStateEvent arg0 ) {
thread2Result = service2.getValue();
}
});
service1.start();
service2.start();

try {
es.shutdown();
es.awaitTermination( 2, TimeUnit.MINUTES );
} catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doitButton.setDisable( false );
lbl_Status.setText( "thread1Result: " + thread1Result + " thread2Result: " + thread2Result );} }

服务示例类

public class ServiceExample extends Service<Boolean> {
private int waitTime;
public ServiceExample( int waitTime ) {
this.waitTime = waitTime;
}
@Override
protected Task<Boolean> createTask() {
return new Task<Boolean>() {
@Override
protected Boolean call() throws Exception {
Thread.sleep(waitTime);
if(waitTime >= 5000){
return true;
}else{
return false;
}
}
};
}}

提前致谢。

最佳答案

电话

es.awaitTermination( 2, TimeUnit.MINUTES );

是一个阻塞调用,因此您不应在 FX 应用程序线程上执行它,因为它会使 UI 无响应。

当服务的任务完成时,Servicestate 属性将为SUCCEEDED。因此,您可以执行以下操作:

private BooleanBinding done ;

public void doIt(ActionEvent event){
Button doitButton = ((Button) event.getSource() );
doitButton.setDisable( true );
ExecutorService es = Executors.newFixedThreadPool(2);
final ServiceExample service1 = new ServiceExample(4000);
final ServiceExample service2 = new ServiceExample(5000);
service1.setExecutor( es );
service2.setExecutor( es );

done =
service1.stateProperty().isEqualTo(Worker.State.SUCCEEDED)
.and(service2.stateProperty().isEqualTo(Worker.State.SUCCEEDED));

done.addListener((obs, wasDone, isNowDone) -> {
if (isNowDone.booleanValue()) {
lbl_Status.setText( "thread1Result: " + service1.getValue() + "thread2Result: " + service2.getValue() );
doitButton.setDisable(false);
}
});

service1.start();
service2.start();
}

关于java - 如何从 2 个独立的 JavaFX 服务线程中获取结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38383751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com