Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda