JavaFX Task线程显然未同时运行

时间:2018-10-22 22:36:22

标签: java multithreading javafx

我一直试图在场景之间放置一个加载场景,我的想法是:

  1. 启动线程以创建所需的场景;
  2. 创建场景的方法还将创建的场景设置为类的Scene属性;
  3. 线程正在运行时,正在创建和设置加载场景。
  4. 线程完成后,将场景设置为所需的场景。

显然,整个加载场景都将被忽略,而所需的场景则只是直接设置。我敢打赌,线程不能同时工作。

这是任务部分和相关属性。

private Stage pokeWindow = new Stage();
    private Scene currentScene;
    private boolean sceneReady = false;
    private String targetScene;
    private int genericInt;

    private final Task setGenericScene = new Task(){
        @Override
        protected Object call() throws Exception {
           sceneReady = false;

        if(targetScene.equals("summaryScene")){
            setSummaryScene(genericInt);
            sceneReady = true;
        }

        return null;
        }  
    };

这是加载场景。

public void setLoadingScene(){
       Thread loadScreen = new Thread(setGenericScene);
        loadScreen.start();


        if(!sceneReady){
        GridPane background = new GridPane();
        BackgroundImage loadingBG = new BackgroundImage(new Image((new File("src/resources/tiles/pokedexfullbg.png")).toURI().toString()), BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        background.setBackground(new Background(loadingBG));

    [...]
    [more javafx code regarding the loading scene]

    Scene loadingScene = new Scene(background, 512, 416);
        this.pokeWindow.setScene(loadingScene);

        animation1.play();
        animation2.play();
        animation3.play();
        animation4.play();
        animation5.play();
        animation6.play();
        animation7.play();
        animation8.play();

        while(!sceneReady){}
        }

        this.pokeWindow.setScene(currentScene);
    }

0 个答案:

没有答案