Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
SecureRandom stream weird behavior with multiple threads

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!