public final static <T> Stream<T> jitter(final Stream<T> stream, final long jitterInNanos) {
        final Iterator<T> it = stream.iterator();
        final Random r = new Random();
        return Streams.stream(new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public T next() {
                final T nextValue = it.next();
                try {
                    final long elapsedNanos = (long) (jitterInNanos * r.nextDouble());
                    final long millis = elapsedNanos / 1000000;
                    final int nanos = (int) (elapsedNanos - millis * 1000000);
                    Thread.sleep(Math.max(0, millis), Math.max(0, nanos));

                } catch (final InterruptedException e) {
                    throw ExceptionSoftener.throwSoftenedException(e);
                }
                return nextValue;
            }

        });
    }
public final static < T > Stream < T > jitter ( final Stream < T > stream , final long jitterInNanos ) { final Iterator < T > it = stream . iterator ( ) ; final Random r = new Random ( ) ; return Streams . stream ( new Iterator < T > ( ) { @ Override public boolean hasNext ( ) { return it . hasNext ( ) ; } @ Override public T next ( ) { final T nextValue = it . next ( ) ; try { final long elapsedNanos = ( long ) ( jitterInNanos * r . nextDouble ( ) ) ; final long millis = elapsedNanos / 1000000 ; final int nanos = ( int ) ( elapsedNanos - millis * 1000000 ) ; Thread . sleep ( Math . max ( 0 , millis ) , Math . max ( 0 , nanos ) ) ; } catch ( final InterruptedException e ) { throw ExceptionSoftener . throwSoftenedException ( e ) ; } return nextValue ; } } ) ; }
Introduce a random jitter / time delay between the emission of elements

@see ReactiveSeq#jitter(long)

@param stream  Stream to introduce jitter to
@param jitterInNanos Max jitter period - random number less than this is used for each jitter
@return Jittered Stream