I'm trying to generate random values using SecureRandom
, specifically its support of streams. Ideally the values should be generated on a constant basis, so the stream could be infinite:
SecureRandom secureRandom = new SecureRandom();
Iterator<Integer> idIterator = secureRandom.ints().distinct().iterator();
The documentation states that "SecureRandom
objects are safe for use by multiple concurrent threads." However when multiple threads retrieve the next value from the iterator, I get an error in (at least) one of the threads that appears to be due to a race condition:
Thread t1 = new Thread(() -> idIterator.next());
Thread t2 = new Thread(() -> idIterator.next());
t1.start();
t2.start();
Exception in thread "Thread-1" java.lang.IllegalStateException: source already consumed or closed
at java.base/java.util.stream.AbstractPipeline.sourceSpliterator(AbstractPipeline.java:409)
at java.base/java.util.stream.AbstractPipeline.lambda$spliterator$0(AbstractPipeline.java:367)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.init(StreamSpliterators.java:142)
at java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:157)
at java.base/java.util.stream.StreamSpliterators$IntWrappingSpliterator.tryAdvance(StreamSpliterators.java:358)
at java.base/java.util.Spliterators$2Adapter.hasNext(Spliterators.java:726)
at java.base/java.util.Spliterators$2Adapter.nextInt(Spliterators.java:732)
at java.base/java.util.PrimitiveIterator$OfInt.next(PrimitiveIterator.java:128)
at java.base/java.util.PrimitiveIterator$OfInt.next(PrimitiveIterator.java:86)
at example.Example.foo(Example.java:39)
When I run the code several times, I sometimes get another kind of exception (NullPointerException).
The behavior is the same if I limit the stream and remove the distinct()
operation:
secureRandom.ints().limit(100).iterator();
EDIT:
On the other hand, if I avoid using a stream and just call SecureRandom.nextInt()
from each thread, no race condition is observed as expected.
Thread t1 = new Thread(() -> secureRandom.nextInt());
Thread t2 = new Thread(() -> secureRandom.nextInt());
t1.start();
t2.start(); // code is thread-safe
I'm wondering why the iterator changes behavior? Especially that the Javadocs of the ints()
method states that "a pseudorandom int value is generated as if it's the result of calling the method nextInt()
".
P.S.: Of course I can solve this but synchronizing the threads acquiring the next value.
While SecureRandom
itself is thread-safe, streams are not. The entire Streams API was built to be accessed by a single thread. While intermediate operations may be executed in parallel, they must be called from a single thread.
Therefore neither ints()
nor its iterator are thread-safe.
So what you can do, is create one stream per thread.
Thread t1 = new Thread(() -> secureRandom.ints().distinct().iterator().next());
Thread t2 = new Thread(() -> secureRandom.ints().distinct().iterator().next());
t1.start();
t2.start();