一个javaFX服务只运行一次while(true)

时间:2015-07-03 12:37:20

标签: java multithreading javafx

我使用服务来移动我的相机(个人类,而不是javaFX相机)但我的while(true)只运行一次:

Service<Void> moveService = new Service<Void>(){
        protected Task<Void> createTask() {
            return new Task<Void>(){
                protected Void call(){
                    while(true){
                        System.out.println("move");
                        if(up) camera.getLocation().x+=speed;
                        if(down) camera.getLocation().x-=speed;
                        if(right) camera.getLocation().y+=speed;
                        if(left) camera.getLocation().y-=speed;
                        try {
                            this.wait(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
        }
    };
    moveService.start();

为什么“run”只显示一次?并且true不是可接受的条件,我怎么知道我的应用程序是否仍在运行? 谢谢你的帮助

编辑:
相机没有初始化,我的不好 但现在我在这一行上有一个例外:

this.getChildren().add(new Text(10, 10, this.location.toString()));

java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.Parent$2.onProposedChange(Unknown Source)
at com.sun.javafx.collections.VetoableListDecorator.add(Unknown Source)
at display.Camera.display(Camera.java:63)
at display.WindowInspection$3$1.call(WindowInspection.java:98)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$160/944212290.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/49730943.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我的方法显示来自我的类​​Camera,它扩展了Pane(JavaFX)

编辑2:
这个

try {
    this.wait(100);
} catch (Exception e) {
    e.printStackTrace();
}

给我那个

java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at display.WindowInspection$3$1.call(WindowInspection.java:103)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$161/2101016123.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/1062036711.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at display.WindowInspection$3$1.call(WindowInspection.java:103)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$161/2101016123.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/1062036711.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at display.WindowInspection$3$1.call(WindowInspection.java:103)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$161/2101016123.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/1062036711.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at display.WindowInspection$3$1.call(WindowInspection.java:103)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$161/2101016123.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/1062036711.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at display.WindowInspection$3$1.call(WindowInspection.java:103)
at display.WindowInspection$3$1.call(WindowInspection.java:1)
at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javafx.concurrent.Service.lambda$null$488(Unknown Source)
at javafx.concurrent.Service$$Lambda$161/2101016123.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.concurrent.Service.lambda$executeTask$489(Unknown Source)
at javafx.concurrent.Service$$Lambda$158/1062036711.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

0 个答案:

没有答案