abs.api.ContextInbox Maven / Gradle / Ivy
package abs.api;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* An {@link Inbox} implementation and manages all the
* {@link ObjectInbox} inside one {@link LocalContext}.
*/
class ContextInbox extends AbstractInbox {
/**
* We allow the receiver of an {@link Envelope} to be
* null
. Thus, the {@link ObjectInbox} for such
* receiver is represented by this value.
*/
private static final Object NULL_RECEIVER = new Object();
private static final ObjectInbox NULL_RECEIVER_INBOX = new ObjectInbox(NULL_RECEIVER, null);
/**
* A dedicated that goes through all {@link ObjectInbox} in
* the {@link Context} and executes the next {@link Envelope}
* if there's any. The {@link #run()} delegates to
* {@link ObjectInbox#run()}.
*/
static class InboxSweeperThread extends Thread {
private final AtomicBoolean running = new AtomicBoolean(false);
/**
* Ctor
*
* @param sweepRunnable the sweeping task
*/
public InboxSweeperThread(Runnable sweepRunnable) {
super(sweepRunnable, "inbox-sweeper");
setDaemon(false);
start();
}
@Override
public void run() {
if (!running.compareAndSet(false, true)) {
return;
}
while (running.get()) {
try {
sleep(0, 1);
super.run();
} catch (Throwable e) {
}
}
}
@Override
public void interrupt() {
running.getAndSet(false);
super.interrupt();
}
}
private final ConcurrentMap