Math.random () method can randomly generate a [0, 1) direct number, including 0, excluding 1
Generate Integer # 0 to 10 # using Math.Round (Math.random () *)) Executorservice Executorservice = Executors.newfixedthreadpool (10); for (int i = 0; i < i++) {executorservice.submit ()-> System.out.println (Math.Round (Math.random () * 10)) ; } executorservice.shutdown ();
You can also use the new Java.util.Random (). Nextint (11) method to generate integers between 0 and 10, Java.util.Random is thread safe, so there is no risk that multiple thread calls can damage seed (seed), but use The Random class needs to create an instance executorservice Executorservice = Executors.newfixedthreadpool (10); for (int i = 0; i < i++) {Executorservice.submit (()-> System.out.println (New Random (). Nextint ())} EX Ecutorservice.shutdown ();
The Random class Java.util.concurrent.ThreadLocalRandom, which provides a single instance/static access in Java 7, is as flexible as math.random () and faster than any other method of handling high concurrency. Java.util.concurrent.ThreadLocalRandom.current (). Nextint (11)//thread isolation produces random numbers executorservice Executorservice = Executors.newfixedthreadpool (11); for (int i = 0; i < i++) {executorservice.submit ()-> System.out.println (Thread.CurrentThread (). GetName () + ":" + threadlocalrandom.current (). Nextint (10));} Executorservice.shutdown ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service