Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of these correctly obtains a random number generator intended for concurrent use?

  • A.ThreadLocalRandom.nextInt(10)
  • B.new Random().nextThreadLocalInt(10)
  • C.ThreadLocalRandom.current().nextInt(10)
  • D.new ThreadLocalRandom().nextInt(10)

Answer: C

Correct answer: ThreadLocalRandom.current().nextInt(10)

ThreadLocalRandom gives each thread its own generator, which removes the contention that a shared Random suffers under concurrency. The instance is reached through the static current(); the class cannot be instantiated directly, and the generating methods are not themselves static.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.