I have a simple java application which calculates prime numbers up to a certain user-given number and prints out the numbers. I've built upon this to include four separate threads which iterate through 4 separate ranges of numbers. Once all 4 threads have completed their iterations I want to print the final ArrayList.
I'm currently getting a ConcurrentModificationException because after I start the 4 threads, the next line of code is the print statement which is trying to print the ArrayList which is at that moment being modified by at least one of the still active threads.
Therefore, I want to be able to have the print statement execute after all 4 threads have died. Furthermore, I would like to do this without using a spinning loop. I have nothing in particular against using a spinning loop except that I imagine there is a better way to do this and I would probably have to assign greater priorities to the 4 threads in order to prevent the spinning loop from using up a significant amount of the CPU.