删除 InvocationDispatcher 中的 synchronized 块 (#4550)
This commit is contained in:
@@ -21,15 +21,14 @@ import java.util.concurrent.Executor;
|
|||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/// When [#accept(T)] is called, this class invokes the handler on another thread.
|
||||||
* When {@link #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,
|
||||||
* 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.
|
||||||
* the handler will only be invoked once, taking the latest argument as its input.
|
///
|
||||||
*
|
/// @author yushijinhun
|
||||||
* @author yushijinhun
|
|
||||||
*/
|
|
||||||
public final class InvocationDispatcher<T> implements Consumer<T> {
|
public final class InvocationDispatcher<T> implements Consumer<T> {
|
||||||
|
|
||||||
|
/// @param executor The executor must dispatch all tasks to a single thread.
|
||||||
public static <T> InvocationDispatcher<T> runOn(Executor executor, Consumer<T> action) {
|
public static <T> InvocationDispatcher<T> runOn(Executor executor, Consumer<T> action) {
|
||||||
return new InvocationDispatcher<>(executor, action);
|
return new InvocationDispatcher<>(executor, action);
|
||||||
}
|
}
|
||||||
@@ -47,9 +46,10 @@ public final class InvocationDispatcher<T> implements Consumer<T> {
|
|||||||
public void accept(T t) {
|
public void accept(T t) {
|
||||||
if (pendingArg.getAndSet(new Holder<>(t)) == null) {
|
if (pendingArg.getAndSet(new Holder<>(t)) == null) {
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
synchronized (InvocationDispatcher.this) {
|
// If the executor supports multiple underlying threads,
|
||||||
action.accept(pendingArg.getAndSet(null).value);
|
// we need to add synchronization, but for now we can omit it :)
|
||||||
}
|
// synchronized (InvocationDispatcher.this)
|
||||||
|
action.accept(pendingArg.getAndSet(null).value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user