ITADN

Replace infinite loop thread with ScheduledExecutorService for SystemPropertyWatcher

#563Closedkamilkrzywanski 创建于 2025-10-16
K
kamilkrzywanskicommented
Description: Currently, the SystemPropertyWatcher in UNIXToolkit is implemented using an infinite loop inside a daemon thread (only in jetbrains runtime) ``` systemPropertyWatcher = InnocuousThread.newThread("SystemPropertyWatcher", () -> { while (true) { try { int isSystemDarkColorScheme = isSystemDarkColorScheme(); if (isSystemDarkColorScheme >= 0) { setDesktopProperty(OS_THEME_IS_DARK, isSystemDarkColorScheme != 0); } Thread.sleep(1000); } catch (Exception ignored) { } } }); systemPropertyWatcher.setDaemon(true); systemPropertyWatcher.start(); ``` This approach can be improved by using a ScheduledExecutorService, which provides better thread management and more robust scheduling: ``` ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(r -> { var thread = InnocuousThread.newThread("SystemPropertyWatcher", r); thread.setDaemon(true); return thread; }); executor.scheduleAtFixedRate(() -> { try { int isSystemDarkColorScheme = isSystemDarkColorScheme(); if (isSystemDarkColorScheme >= 0) { setDesktopProperty(OS_THEME_IS_DARK, isSystemDarkColorScheme != 0); } } catch (Exception ignored) { ignored.printStackTrace(); } }, 0, 1, TimeUnit.SECONDS); ``` Eliminates the need for an explicit infinite loop and Thread.sleep(). Provides better thread lifecycle management. This change aligns the implementation with modern Java concurrency best practices.
关闭于 2025-10-17 2 条评论