Avoid allocating a wrapper for a runnable every time an actor is scheduled

This commit is contained in:
Tony Garnock-Jones 2023-10-28 23:16:12 +02:00
parent 90147b4970
commit e483a57df9
1 changed files with 6 additions and 4 deletions

View File

@ -13,10 +13,10 @@ import java.util.logging.Logger;
/** /**
* I represent the shared execution concepts for a collection of objects; I am roughly analogous to the E concept of a Vat. * I represent the shared execution concepts for a collection of objects; I am roughly analogous to the E concept of a Vat.
*/ */
public class Actor { public class Actor extends ForkJoinTask<Void> {
private final static AtomicLong _count = new AtomicLong(0); private final static AtomicLong _count = new AtomicLong(0);
private final static AtomicLong _actorId = new AtomicLong(0); private final static AtomicLong _actorId = new AtomicLong(0);
protected final static ExecutorService _executor = new ForkJoinPool( protected final static ForkJoinPool _executor = new ForkJoinPool(
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, null,
@ -212,12 +212,14 @@ public class Actor {
} }
} }
private Runnable __runWorkItems = this::_runWorkItems; public final Void getRawResult() { return null; }
public final void setRawResult(Void v) { }
public final boolean exec() { this._runWorkItems(); return false; }
public void execute(WorkItem item) { public void execute(WorkItem item) {
tail.getAndSet(item).set(item); tail.getAndSet(item).set(item);
if (workItemCount.getAndIncrement() == 0) { if (workItemCount.getAndIncrement() == 0) {
_executor.execute(__runWorkItems); _executor.execute(this);
} }
} }