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

  1. the main thread creates latch number of threads.
  2. the main thread starts working threads.
    1. each thread has reference latch.
    2. it counts latch down when finishes work.
  3. the main thread waits latch count down 0.
  4. the main thread printing job.

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -