如何解决java fx折旧线程导致gui冻结的问题?

ovfsdjhp  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(190)

我正在用JavaFX写一个项目-洗衣机。
它的工作非常好,但我有一些线程的问题。我的ide显示我使用的线程方法 suspend() 以及 resume() 折旧。
我想这是一个问题,因为我的洗衣机需要旋转衣服,但它完全冻结随机。有时它会恢复旋转,有时不会,我必须重新启动应用程序。
我知道这可能需要一些代码来检查(~100行),但是你可以 ctrl+f 通过它找到 resume() 以及 suspend() . 程序的逻辑相当简单,我已经对正在发生的事情发表了评论。

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

import java.util.concurrent.TimeUnit;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
public class Controller {
    @FXML
    private Button button;
    @FXML
    private AnchorPane pranko;
    @FXML
    private Label score;
    private Thread thread;
    private Boolean on = false;
    private int rotations = 0;
    //sound set-up
    String musicFile = "snap.mp3";
    Media sound = new Media(new File(musicFile).toURI().toString());
    MediaPlayer mediaPlayer = new MediaPlayer(sound);
    @FXML
    private void initialize() throws InterruptedException {
        //Start/Stop button initialize
        Circle circle = new Circle(25);
        button.setShape(circle);
        button.setMinSize(50,50);
        //LCD screen set-up
        score.setStyle("-fx-text-fill: green;" +
                "-fx-background-color: black");
        score.setText("START");
        //adding shadow around the button
        DropShadow dropShadow = new DropShadow();
        dropShadow.setRadius(5);
        dropShadow.setColor(Color.GREEN);
        button.setEffect(dropShadow);
        // Rotation and main app logic <<<<<<<<<<<<<<<<<<<<<<<<
        thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    for (int i = 0; i < 360; i++) {
                        pranko.setRotate(i);
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                score.setText(String.valueOf(rotations));
                            }
                        });
                        try {
                            //time between iterations so the rotation could work.
                            TimeUnit.MILLISECONDS.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (rotations == 0) {
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                on = false;
                                //Changing text in button, it's color and shadow
                                button.setText("start");
                                button.setStyle("-fx-text-fill: #0cf800");
                                DropShadow dropShadow = new DropShadow();
                                dropShadow.setRadius(5);
                                dropShadow.setColor(Color.GREEN);
                                button.setEffect(dropShadow);
                                //text change in LCD screen
                                score.setStyle("-fx-text-fill: red;" +
                                        "-fx-background-color: black");
                                score.setText("END");

                            }
                        });
                        thread.suspend();
                    } else {
                        rotations -= 1;

                    }
                }
            }
        };
        thread.start();
        thread.suspend();
    }

    @FXML
    private void toggle() throws InterruptedException {
        if (on) {
            on = false;
            thread.suspend();
            //changing text in button, it's color and shadow
            button.setText("start");
            button.setStyle("-fx-text-fill: #0cf800");
            DropShadow dropShadow = new DropShadow();
            dropShadow.setRadius(5);
            dropShadow.setColor(Color.GREEN);
            button.setEffect(dropShadow);
        } else {
            thread.resume();
            on = true;
            //changing text in button, it's color and shadow
            button.setText("stop");
            button.setStyle("-fx-text-fill: red");
            DropShadow dropShadow = new DropShadow();
            dropShadow.setRadius(5);
            dropShadow.setColor(Color.RED);
            button.setEffect(dropShadow);
            rotations = 10; //set full rotation count
            //changing color of text in LCD and the background
            score.setStyle("-fx-text-fill: green;" +
                    "-fx-background-color: black");
        }
    }
}

完整的项目包可在以下位置获得:https://drive.google.com/file/d/149r-fnss7nymqj_igukl4dkfe6putxmw/view?usp=sharing 病毒扫描:https://www.virustotal.com/gui/file/d70d900c3a75dbc58f089358d33b275046ff5879e7aa16df427b09ae3cb96093/detection
我已经花了很多时间了,所以我请求帮助。每一次帮助都会得到回报。
必须有一种方法使它正常地与线程一起工作或以其他方式工作

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题