From 65a4b6159dbe5c03f9dee8db1950e16a36453be8 Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 23 Jan 2026 22:18:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0=E5=86=85?= =?UTF-8?q?=E5=AD=98=E4=BF=A1=E6=81=AF=E4=BB=BB=E5=8A=A1=20(#5286)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hmcl/ui/versions/VersionSettingsPage.java | 82 +++++++++++++------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java index 6b39dd823..f706c2cfc 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java @@ -42,10 +42,7 @@ import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.WeakListenerHolder; import org.jackhuang.hmcl.ui.construct.*; import org.jackhuang.hmcl.ui.decorator.DecoratorPage; -import org.jackhuang.hmcl.util.Lang; -import org.jackhuang.hmcl.util.Pair; -import org.jackhuang.hmcl.util.ServerAddress; -import org.jackhuang.hmcl.util.StringUtils; +import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.javafx.BindingMapping; import org.jackhuang.hmcl.util.javafx.PropertyUtils; import org.jackhuang.hmcl.util.javafx.SafeStringConverter; @@ -56,34 +53,18 @@ import org.jackhuang.hmcl.util.platform.SystemInfo; import org.jackhuang.hmcl.util.platform.hardware.PhysicalMemoryStatus; import org.jackhuang.hmcl.util.versioning.GameVersionNumber; +import java.lang.ref.WeakReference; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import static org.jackhuang.hmcl.ui.FXUtils.stringConverter; import static org.jackhuang.hmcl.util.DataSizeUnit.GIGABYTES; import static org.jackhuang.hmcl.util.DataSizeUnit.MEGABYTES; -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 memoryStatus = new SimpleObjectProperty<>(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(SystemInfo.getPhysicalMemoryStatus())); - } - }; - getTimer().scheduleAtFixedRate(memoryStatusUpdateTask, 0, 1000); - } - private final ReadOnlyObjectWrapper state = new ReadOnlyObjectWrapper<>(new State("", null, false, false, false)); private AdvancedVersionSettingPage advancedVersionSettingPage; @@ -127,6 +108,8 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag private final IntegerProperty maxMemory = new SimpleIntegerProperty(); private final BooleanProperty modpack = new SimpleBooleanProperty(); + private final ReadOnlyObjectProperty memoryStatus = UpdateMemoryStatus.memoryStatusProperty(); + public VersionSettingsPage(boolean globalSetting) { ScrollPane scrollPane = new ScrollPane(); scrollPane.setFitToHeight(true); @@ -510,10 +493,7 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag cboProcessPriority.getItems().setAll(ProcessPriority.values()); cboProcessPriority.setConverter(stringConverter(e -> i18n("settings.advanced.process_priority." + e.name().toLowerCase(Locale.ROOT)))); - memoryStatus.set(SystemInfo.getPhysicalMemoryStatus()); componentList.disableProperty().bind(enableSpecificSettings.not()); - - initMemoryStatusUpdateTask(); } @Override @@ -809,4 +789,58 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag public ReadOnlyObjectProperty stateProperty() { return state.getReadOnlyProperty(); } + + private static final class UpdateMemoryStatus extends Thread { + + @FXThread + private static WeakReference> memoryStatusPropertyCache; + + @FXThread + static ReadOnlyObjectProperty memoryStatusProperty() { + if (memoryStatusPropertyCache != null) { + var property = memoryStatusPropertyCache.get(); + if (property != null) { + return property; + } + } + + ObjectProperty property = new SimpleObjectProperty<>(PhysicalMemoryStatus.INVALID); + memoryStatusPropertyCache = new WeakReference<>(property); + new UpdateMemoryStatus(memoryStatusPropertyCache).start(); + return property; + } + + private final WeakReference> memoryStatusPropertyRef; + + UpdateMemoryStatus(WeakReference> memoryStatusPropertyRef) { + this.memoryStatusPropertyRef = memoryStatusPropertyRef; + + setName("UpdateMemoryStatus"); + setDaemon(true); + setPriority(Thread.MIN_PRIORITY); + } + + @Override + public void run() { + while (true) { + PhysicalMemoryStatus status = SystemInfo.getPhysicalMemoryStatus(); + + var memoryStatusProperty = memoryStatusPropertyRef.get(); + if (memoryStatusProperty == null) + return; + + if (Controllers.isStopped()) + return; + + Platform.runLater(() -> memoryStatusProperty.set(status)); + + try { + //noinspection BusyWait + Thread.sleep(1000); + } catch (InterruptedException e) { + return; + } + } + } + } }