java - How do I schedule a print statement upon completion of 4 independent threads? -
i have simple java application calculates prime numbers user-given number , prints out numbers. i've built upon include 4 separate threads iterate through 4 separate ranges of numbers. once 4 threads have completed iterations want print final arraylist.
i'm getting concurrentmodificationexception because after start 4 threads, next line of code print statement trying print arraylist @ moment being modified @ least 1 of still active threads.
therefore, want able have print statement execute after 4 threads have died. furthermore, without using spinning loop. have nothing in particular against using spinning loop except imagine there better way , have assign greater priorities 4 threads in order prevent spinning loop using significant amount of cpu.
use countdownlatch
. javadoc class tells how have
- the main thread creates latch number of threads.
- the main thread starts working threads.
- each thread has reference latch.
- it counts latch down when finishes work.
- the main thread waits latch count down 0.
- the main thread printing job.
Comments
Post a Comment