From 938d8647ce5c2211ba5182f68f3dde8eb5cd18e4 Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 26 Sep 2025 20:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20InvocationDispatcher=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20synchronized=20=E5=9D=97=20(#4550)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hmcl/util/InvocationDispatcher.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java index 3bfa67a9e..a6cf8031b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java @@ -21,15 +21,14 @@ import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; -/** - * When {@link #accept(T)} is called, this class invokes the handler on another thread. - * If {@link #accept(T)} is called more than one time before the handler starts processing, - * the handler will only be invoked once, taking the latest argument as its input. - * - * @author yushijinhun - */ +/// When [#accept(T)] is called, this class invokes the handler on another thread. +/// If [#accept(T)] is called more than one time before the handler starts processing, +/// the handler will only be invoked once, taking the latest argument as its input. +/// +/// @author yushijinhun public final class InvocationDispatcher implements Consumer { + /// @param executor The executor must dispatch all tasks to a single thread. public static InvocationDispatcher runOn(Executor executor, Consumer action) { return new InvocationDispatcher<>(executor, action); } @@ -47,9 +46,10 @@ public final class InvocationDispatcher implements Consumer { public void accept(T t) { if (pendingArg.getAndSet(new Holder<>(t)) == null) { executor.execute(() -> { - synchronized (InvocationDispatcher.this) { - action.accept(pendingArg.getAndSet(null).value); - } + // If the executor supports multiple underlying threads, + // we need to add synchronization, but for now we can omit it :) + // synchronized (InvocationDispatcher.this) + action.accept(pendingArg.getAndSet(null).value); }); } }