首页 > 解决方案 > 如何解决导致 GUI 冻结的 Java fx 折旧线程的问题?

问题描述

我正在用 Java Fx - 洗衣机编写一个项目。

它工作得很好,但我有一些线程问题。我的 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/d70d900c3a75dbc58f089358d33b275046ff5879e7aa16df427b09ae3c/检测

我已经花了很多时间,所以我正在寻求帮助。每一次帮助都会得到回报。

必须有一种方法可以使其与线程或其他方式正常工作

标签: javamultithreadingjavafx

解决方案


推荐阅读