问题
I've got problem with these piece of code. I want to make an application, which constatnly displays randomized values in labels as long as the Toggle Button is pressed. That's what I created, it works but window after a few seconds is lagging horrible. What am I doing wrong?
Here is code of class, which generate random values:
public class ValueMaker{
private StringPropterty x, y, z;
private Random generator;
private boolean isStarted = false;
private int randomizedX(){ return generator.nextInt(10); }
private int randomizedY(){ return generator.nextInt(10); }
private int randomizedZ(){ return generator.nextInt(10); }
public StringProperty xProperty(){ return x; }
public StringProperty yProperty(){ return y; }
public StringProperty zProperty() { return z; }
public ValueMaker(){
x = new SimpleStringProperty("0");
y = new SimpleStringProperty("0");
z = new SimpleStringProperty("0");
generator = new Random();
}
private void setValues(){
Platform.runLater(new Runnable() {
@Override
public void run() {
x.set(String.valueOf(randomizedX()));
y.set(String.valueOf(randomizedY()));
z.set(String.valueOf(randomizedZ()));
}
});
}
public startRandomize(){
isStarted = true;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (isStarted){
setValues();
}
}
});
t.start();
}
public stopRandomize(){
isStarted = false;
}
}
And here is piece of Controller code, which calls these methods:
@FXML
private void initialize(){
labelX.textProperty().bind(ValueMaker.xProperty());
labelY.textProperty().bind(ValueMaker.yProperty());
labelZ.textProperty().bind(ValueMaker.zProperty());
startRandomizeButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if (newValue==true){
ValueMaker.startRandomize();
}
else{
ValueMaker.stopRandomize();
}
}
});
}
回答1:
You are jamming the JavaFX application thread by calling Platform.runLater
continuously. The best way to achieve this would be to use a AnimationTimer which will be called in each frame while it is active.
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
x.set(String.valueOf(randomizedX()));
y.set(String.valueOf(randomizedY()));
z.set(String.valueOf(randomizedZ()));
}
};
and then call the timer in your example :
startRandomizeButton.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
// newValue==true is not required
if (newValue){
timer.start();
}
else{
timer.stop();
}
});
}
回答2:
I think when you start your thread - it uses all available resources. It works in an infinite loop and generates random numbers without taking a "break".
Try adding sleep
to the thread's loop, so that it will be getting suspended for few milliseconds and releasing CPU a little.
You may need to add sleep anyway, otherwise the randomized labels will change too fast.
来源:https://stackoverflow.com/questions/29941093/javafx-concurrency-and-updating-label