Search in sources :

Example 1 with DispatcherThread

use of org.nustaq.kontraktor.impl.DispatcherThread in project kontraktor by RuedigerMoeller.

the class Promise method awaitPromise.

/**
 * see IPromise (inheriting Callback) interface
 * @param timeout
 */
@Override
public IPromise<T> awaitPromise(long timeout) {
    long endtime = 0;
    if (timeout > 0) {
        endtime = System.currentTimeMillis() + timeout;
    }
    if (Thread.currentThread() instanceof DispatcherThread) {
        DispatcherThread dt = (DispatcherThread) Thread.currentThread();
        Scheduler scheduler = dt.getScheduler();
        int idleCount = 0;
        dt.__stack.add(this);
        while (!isSettled()) {
            if (!dt.pollQs()) {
                idleCount++;
                scheduler.pollDelay(idleCount);
            } else {
                idleCount = 0;
            }
            if (endtime != 0 && System.currentTimeMillis() > endtime && !isSettled()) {
                timedOut(Timeout.INSTANCE);
                break;
            }
        }
        dt.__stack.remove(dt.__stack.size() - 1);
        return this;
    } else {
        // if outside of actor machinery, just block
        CountDownLatch latch = new CountDownLatch(1);
        then((res, err) -> {
            latch.countDown();
        });
        boolean timedOut = false;
        try {
            timedOut = !latch.await(timeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (timedOut)
            timedOut(Timeout.INSTANCE);
        return this;
    }
}
Also used : DispatcherThread(org.nustaq.kontraktor.impl.DispatcherThread) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 DispatcherThread (org.nustaq.kontraktor.impl.DispatcherThread)1