package com.lmax.disruptor;

import java.util.concurrent.TimeUnit;

import static com.lmax.disruptor.util.Util.awaitNanos;

public class TimeoutBlockingWaitStrategy implements WaitStrategy
{
    private final Object mutex = new Object();
    private final long timeoutInNanos;

    public TimeoutBlockingWaitStrategy(final long timeout, final TimeUnit units)
    {
        timeoutInNanos = units.toNanos(timeout);
    }

    @Override
    public long waitFor(
        final long sequence,
        final Sequence cursorSequence,
        final Sequence dependentSequence,
        final SequenceBarrier barrier)
        throws AlertException, InterruptedException, TimeoutException
    {
        long timeoutNanos = timeoutInNanos;

        long availableSequence;
        if (cursorSequence.get() < sequence)
        {
            synchronized (mutex)
            {
                while (cursorSequence.get() < sequence)
                {
                    barrier.checkAlert();
                    timeoutNanos = awaitNanos(mutex, timeoutNanos);
                    if (timeoutNanos <= 0)
                    {
                        throw TimeoutException.INSTANCE;
                    }
                }
            }
        }

        while ((availableSequence = dependentSequence.get()) < sequence)
        {
            barrier.checkAlert();
        }

        return availableSequence;
    }

    @Override
    public void signalAllWhenBlocking()
    {
        synchronized (mutex)
        {
            mutex.notifyAll();
        }
    }

    @Override
    public String toString()
    {
        return "TimeoutBlockingWaitStrategy{" +
            "mutex=" + mutex +
            ", timeoutInNanos=" + timeoutInNanos +
            '}';
    }
}

@@ -117,16 +117,530 @@
 Nanos;%0A%0A
+/**%0A * Blocking strategy that uses a lock and condition variable for %7B@link EventProcessor%7Ds waiting on a barrier.%0A * However it will periodically wake up if it has been idle for specified period by throwing a%0A * %7B@link TimeoutException%7D.  To make use of this, the event handler class should implement the %7B@link TimeoutHandler%7D,%0A * which the %7B@link BatchEventProcessor%7D will call if the timeout occurs.%0A * %3Cp%3E%0A * This strategy can be used when throughput and low-latency are not as important as CPU resource.%0A */%0A
 public c

Add javadoc for TimeoutBlockingWaitStrategy, fixes #229.