JavaFx,多线程应用程序,奇怪的异常

标签 java multithreading user-interface javafx-2 javafx

我正在开发 JavaFx 应用程序。应用程序创建多个线程(同一类)。 线程在这里创建:

for (i = 0; i < turtlesNumber; i++) {
        TurtleRunThread turtleRunThread = new TurtleRunThread();

        turtleRunThread.setController(this.controller);
        turtleRunThread.setTurtle(this.model.getTurtles().get(i));
        turtleRunThread.setFinish(this.mainUI.getRacePane().getWidth() - 47.3);

        turtleRunThread.start();

        threadArrayList.add(turtleRunThread);
    }

这是线程类的run方法:

     public void run() {
    //this.controller.run(this.turtle, this.finish);

    long speed = 500;
    double step = 20; //10 pixeli

    while (turtle.getX() < finish) {
        Random random = new Random();

        controller.modifyTurtlePosition(turtle, speed, step);

        if (Math.random() < 0.2) {
            if (Math.random() < 0.5) {
                if (speed > 100)
                    speed -= random.nextLong() % (((double)1 / 3) * speed);
            } else {
                if (speed < 1500)
                    speed += random.nextLong() % (((double)1 / 3) * speed);
            }
        }
    }
}

这是 Controller 方法“modifyTurtlePositions(...)”:

    public void modifyTurtlePosition(final ImageView turtle, long speed, double step) {
    Timeline timeline = new Timeline();

    final double newX = turtle.getX() + step;

    KeyValue keyValue = new KeyValue(turtle.xProperty(), turtle.getX() + step);
    KeyFrame keyFrame = new KeyFrame(Duration.millis(speed), keyValue);

    timeline.getKeyFrames().add(keyFrame);

    timeline.setOnFinished(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            turtle.setX(newX);
        }
    });

    timeline.play();

    try {
        Thread.sleep(speed);
    } catch (InterruptedException e) {
        System.err.println(e.getMessage());
    }
}

问题是,如果我不同步此方法,则会出现以下异常:

Exception in thread "Thread-16" java.lang.ArrayIndexOutOfBoundsException: 2
at java.util.ArrayList.add(ArrayList.java:412)
at com.sun.scenario.animation.AbstractMasterTimer.addPulseReceiver(AbstractMasterTimer.java:211)
at com.sun.scenario.animation.shared.AnimationPulseReceiver.addPulseReceiver(AnimationPulseReceiver.java:70)
at com.sun.scenario.animation.shared.AnimationPulseReceiver.start(AnimationPulseReceiver.java:84)
at javafx.animation.Animation.play(Animation.java:775)
at javafx.animation.Animation.play(Animation.java:762)
at sample.Controller.modifyTurtlePosition(Controller.java:95)
at sample.TurtleRunThread.run(TurtleRunThread.java:35)
    java.lang.NullPointerException
at     com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:366)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:289)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:456)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:329)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)

你有什么想法吗?

最佳答案

controller.modifyTurtlePosition(turtle, speed, step); 包装到 Platform.runLater() 中:

Platform.runLater(new Runnable() {
    public void run() {
        controller.modifyTurtlePosition(turtle, speed, step);
    }
}

所有 UI 操作必须在 UI 线程上完成。

关于JavaFx,多线程应用程序,奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16221231/

相关文章:

java.lang.ClassCastException : java. lang.Long 无法转换为 clojure.lang.IFn

java - 以编程方式检查 ThreadStackSize?

java - 使用 wait() 和 Notify() 在 4 个线程之间执行线程间通信时出现 IllegalMonitorStateException

java - URISyntaxException 禁用创建新 GUI 的能力

java - 从 JPanel 中删除 JPanel

c++ - gwen + opengl 什么都看不到

Java 或 JavaC 不断提示找不到包或包不存在

java - Stripe invoice.payment_succeeded webhook 被多次触发

java - 文本叠加在图像上 libgdx

java - 线程生命周期