Skip to content

Commit

Permalink
Update memory status every 1s, fix #3440
Browse files Browse the repository at this point in the history
This commit also fixes Lang.getTimer() returns a non-daemon Timer,
preventing Java from exiting after closing the launcher.
  • Loading branch information
yushijinhun committed Dec 30, 2024
1 parent 7d12ef6 commit 78e15d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,27 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static org.jackhuang.hmcl.ui.FXUtils.stringConverter;
import static org.jackhuang.hmcl.util.Lang.getTimer;
import static org.jackhuang.hmcl.util.Pair.pair;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;

public final class VersionSettingsPage extends StackPane implements DecoratorPage, VersionPage.VersionLoadable, PageAware {

private static final ObjectProperty<OperatingSystem.PhysicalMemoryStatus> memoryStatus = new SimpleObjectProperty<>(OperatingSystem.PhysicalMemoryStatus.INVALID);
private static TimerTask memoryStatusUpdateTask;
private static void initMemoryStatusUpdateTask() {
FXUtils.checkFxUserThread();
if (memoryStatusUpdateTask != null)
return;
memoryStatusUpdateTask = new TimerTask() {
@Override
public void run() {
Platform.runLater(() -> memoryStatus.set(OperatingSystem.getPhysicalMemoryStatus()));
}
};
getTimer().scheduleAtFixedRate(memoryStatusUpdateTask, 0, 1000);
}

private final ReadOnlyObjectWrapper<State> state = new ReadOnlyObjectWrapper<>(new State("", null, false, false, false));

private AdvancedVersionSettingPage advancedVersionSettingPage;
Expand Down Expand Up @@ -102,7 +119,6 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag
private final BooleanProperty navigateToSpecificSettings = new SimpleBooleanProperty(false);
private final BooleanProperty enableSpecificSettings = new SimpleBooleanProperty(false);
private final IntegerProperty maxMemory = new SimpleIntegerProperty();
private final ObjectProperty<OperatingSystem.PhysicalMemoryStatus> memoryStatus = new SimpleObjectProperty<>(OperatingSystem.PhysicalMemoryStatus.INVALID);
private final BooleanProperty modpack = new SimpleBooleanProperty();

public VersionSettingsPage(boolean globalSetting) {
Expand Down Expand Up @@ -470,6 +486,8 @@ public VersionSettingsPage(boolean globalSetting) {

memoryStatus.set(OperatingSystem.getPhysicalMemoryStatus());
componentList.disableProperty().bind(enableSpecificSettings.not());

initMemoryStatusUpdateTask();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public static <T, U> void forEachZipped(Iterable<T> i1, Iterable<U> i2, BiConsum

public static synchronized Timer getTimer() {
if (timer == null) {
timer = new Timer();
timer = new Timer(true);
}
return timer;
}
Expand Down

0 comments on commit 78e15d1

Please sign in to comment.