有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java连续线程。sleep()在一个同步的瞬间继续运行理想的延迟代码

我目前正在与JavaFX合作开发一个游戏程序。我在这里计划的是一个加载面板,按照理想的顺序:

  1. 将主舞台切换到加载场景
  2. 等待1秒
  3. 设置显示加载状态的面板
  4. 再等1秒
  5. 创造一个世界

五,。a) 将随机种子分配给世界

五,。b) 等待1秒

五,。c) 设置坐标

五,。d) 等待1秒

五,。e) 分配基本数组

相反,情况是这样的:

  1. 等待4~5s

  2. 执行步骤#1、3、5a、5c、5e

我如何确保程序遵循理想的顺序?我尝试了一个新的线程任务,它成功了,但world构造函数无法遵循线程并导致错误

private static void loading() {
        Main.loading = new Scene(new Loading(), 1280, 720);
        Main.stage.setScene(Main.loading);
        try {
            Thread.sleep(1000);
            ((Loading) Main.loading.getRoot()).status.setVisible(true);
            Thread.sleep(1000);
            Main.currentWorld = new World();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
    }

世界承包商

        seed = ThreadLocalRandom.current().nextInt();
        ((Loading) Main.loading.getRoot()).progress.setProgress(0.1);
        ((Loading) Main.loading.getRoot()).statusTxt.setText("Seed created");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Random coord = new Random(seed);
        position = new Coordinate((coord.nextInt() % 200) + 1, (coord.nextInt() % 200) + 1);
        ((Loading) Main.loading.getRoot()).progress.setProgress(0.2);
        ((Loading) Main.loading.getRoot()).statusTxt.setText("Starting coordinates instantiated");
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        destStr = new ArrayList<Coordinate>();
        consStr = new ArrayList<Coordinate>();
        recStr = new ArrayList<Coordinate>();
        builds = new ArrayList<Coordinate>();
        ((Loading) Main.loading.getRoot()).progress.setProgress(0.3);
        ((Loading) Main.loading.getRoot()).statusTxt.setText("Structure & Building list instantiated");
        

共 (0) 个答案