Cut out a couple of middlemen
This commit is contained in:
parent
935b7ea5c3
commit
864f371034
|
@ -201,7 +201,32 @@ public class Actor implements Executor {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Runnable work, Runnable ifNotAlive) {
|
public void execute(Runnable work, Runnable ifNotAlive) {
|
||||||
this.later(0, work, ifNotAlive);
|
{
|
||||||
|
WorkItem i = new WorkItem(work, ifNotAlive);
|
||||||
|
tail.getAndSet(i).set(i);
|
||||||
|
}
|
||||||
|
if (workItemCount.getAndIncrement() == 0) {
|
||||||
|
_executor.execute(() -> {
|
||||||
|
synchronized (this) {
|
||||||
|
_currentActor.set(this);
|
||||||
|
try {
|
||||||
|
long batch = workItemCount.get();
|
||||||
|
while (batch > 0) {
|
||||||
|
for (int count = 0; count < batch; count++) {
|
||||||
|
WorkItem i = null;
|
||||||
|
while (i == null) i = head.get();
|
||||||
|
head = i;
|
||||||
|
this._perform(i.work, i.ifNotAlive);
|
||||||
|
i.clear();
|
||||||
|
}
|
||||||
|
batch = workItemCount.addAndGet(-batch);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
_currentActor.set(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void later(long delayMilliseconds, Runnable work) {
|
public void later(long delayMilliseconds, Runnable work) {
|
||||||
|
@ -210,32 +235,7 @@ public class Actor implements Executor {
|
||||||
|
|
||||||
public void later(long delayMilliseconds, Runnable work, Runnable ifNotAlive) {
|
public void later(long delayMilliseconds, Runnable work, Runnable ifNotAlive) {
|
||||||
if (delayMilliseconds == 0) {
|
if (delayMilliseconds == 0) {
|
||||||
{
|
this.execute(work, ifNotAlive);
|
||||||
WorkItem i = new WorkItem(work, ifNotAlive);
|
|
||||||
tail.getAndSet(i).set(i);
|
|
||||||
}
|
|
||||||
if (workItemCount.getAndIncrement() == 0) {
|
|
||||||
_executor.execute(() -> {
|
|
||||||
synchronized (this) {
|
|
||||||
_currentActor.set(this);
|
|
||||||
try {
|
|
||||||
long batch = workItemCount.get();
|
|
||||||
while (batch > 0) {
|
|
||||||
for (int count = 0; count < batch; count++) {
|
|
||||||
WorkItem i = null;
|
|
||||||
while (i == null) i = head.get();
|
|
||||||
head = i;
|
|
||||||
this._perform(i.work, i.ifNotAlive);
|
|
||||||
i.clear();
|
|
||||||
}
|
|
||||||
batch = workItemCount.addAndGet(-batch);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
_currentActor.set(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
_scheduledExecutor.schedule(() -> this._performSync(work, ifNotAlive), delayMilliseconds, TimeUnit.MILLISECONDS);
|
_scheduledExecutor.schedule(() -> this._performSync(work, ifNotAlive), delayMilliseconds, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,18 +18,21 @@ public class Remote<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void async(Consumer<T> f) {
|
public void async(Consumer<T> f) {
|
||||||
this.async(0, f);
|
this._actor.execute(() -> f.accept(this._target));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void async(long delayMilliseconds, Consumer<T> f) {
|
public void async(long delayMilliseconds, Consumer<T> f) {
|
||||||
this._actor.later(delayMilliseconds, () -> f.accept(this._target));
|
this._actor.later(delayMilliseconds, () -> f.accept(this._target));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Promise<Object> syncVoid(Consumer<T> f) {
|
public Promise<?> syncVoid(Consumer<T> f) {
|
||||||
return this.syncVoid(0, f);
|
return this.sync((t) -> {
|
||||||
|
f.accept(t);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Promise<Object> syncVoid(long delayMilliseconds, Consumer<T> f) {
|
public Promise<?> syncVoid(long delayMilliseconds, Consumer<T> f) {
|
||||||
return this.sync(delayMilliseconds, (t) -> {
|
return this.sync(delayMilliseconds, (t) -> {
|
||||||
f.accept(t);
|
f.accept(t);
|
||||||
return null;
|
return null;
|
||||||
|
@ -37,7 +40,11 @@ public class Remote<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public<R> Promise<R> sync(Function<T, R> f) {
|
public<R> Promise<R> sync(Function<T, R> f) {
|
||||||
return this.sync(0, f);
|
Promise<R> p = new Promise<>();
|
||||||
|
this._actor.execute(
|
||||||
|
() -> p.resolveWith(f.apply(this._target)),
|
||||||
|
() -> p.rejectWith(this._actor.getExitReason()));
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public<R> Promise<R> sync(long delayMilliseconds, Function<T, R> f) {
|
public<R> Promise<R> sync(long delayMilliseconds, Function<T, R> f) {
|
||||||
|
|
Loading…
Reference in New Issue