Answer by Supun Wijerathne for AtomicInteger in multithreading
Explain me why option (2) is more slowly?Simply because you do it inside run(). So multiple threads will try to do it at the same time hence there will be wait s and release s. Bowmore has given a low...
View ArticleAnswer by Alexey Soshin for AtomicInteger in multithreading
You miss two main points here: what AtomicInteger is for and how multithreading works in general.Regarding why Option 2 is slower, @bowmore provided an excellent answer already.Now regarding printing...
View ArticleAnswer by bowmore for AtomicInteger in multithreading
In the (2) case, up to 11 threads (the ten from the ExecutorService plus the main thread) are contending for access to the AtomicInteger, whereas in case (1) only the main thread accesses it. In fact,...
View ArticleAnswer by Patrick Parker for AtomicInteger in multithreading
Your outer loop is:while (numbers.get() < 1000000)This allows you to continue submitting more Runnables than intended to the ExecutorService in the main thread.You could try changing the loop to:...
View ArticleAtomicInteger in multithreading
I want to find out all the prime numbers from 0 to 1000000. For that I wrote this stupid method:public static boolean isPrime(int n) { for(int i = 2; i < n; i++) { if (n % i == 0) return false; }...
View Article
More Pages to Explore .....