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 fd79ef11d..3bfa67a9e 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/InvocationDispatcher.java @@ -20,36 +20,37 @@ package org.jackhuang.hmcl.util; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; -import java.util.function.Supplier; /** - * When {@link #accept(ARG)} is called, this class invokes the handler on another thread. - * If {@link #accept(ARG)} is called more than one time before the handler starts processing, + * 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 */ -public final class InvocationDispatcher implements Consumer { +public final class InvocationDispatcher implements Consumer { - public static InvocationDispatcher runOn(Executor executor, Consumer action) { - return new InvocationDispatcher<>(arg -> executor.execute(() -> { - synchronized (action) { - action.accept(arg.get()); - } - })); + public static InvocationDispatcher runOn(Executor executor, Consumer action) { + return new InvocationDispatcher<>(executor, action); } - private final Consumer> handler; - private final AtomicReference> pendingArg = new AtomicReference<>(); + private final Executor executor; + private final Consumer action; + private final AtomicReference> pendingArg = new AtomicReference<>(); - public InvocationDispatcher(Consumer> handler) { - this.handler = handler; + private InvocationDispatcher(Executor executor, Consumer action) { + this.executor = executor; + this.action = action; } @Override - public void accept(ARG arg) { - if (pendingArg.getAndSet(new Holder<>(arg)) == null) { - handler.accept(() -> pendingArg.getAndSet(null).value); + public void accept(T t) { + if (pendingArg.getAndSet(new Holder<>(t)) == null) { + executor.execute(() -> { + synchronized (InvocationDispatcher.this) { + action.accept(pendingArg.getAndSet(null).value); + } + }); } } }