For example, I need to always start 100 threads to perform some actions. I have a class called ThreadsWorker that searches for the number of threads and starts the missing threads if some of the previous ones are completed. So this is a table describing the situation:
1 second: 100 threads 2 second: 92 threads (ThreadsWorker generates new 8 threads) 3 second: 100 theads 4 second: 72 threads (ThreadsWorker generates 28 threads)
And so on. My threads are anonymous calls (just new Thread(new Runnable(...)).start() ) because I donβt know how to save them in the Threads[] array correctly, because while ThreadsWorker will save threads[i] = new Threads() , some threads can be finished, and then there will be some collision with the array indices.
Due to anonymous calls, I now use the threadsCount variable and increase it at the beginning of the body and decrease its length (using synchronized ). Well, it works correctly, and the only way is to use a while() , which checks if threadsCount == 0 when progress is complete.
I think this is C-style, but not the Java way :) So, can you help me do this in the Java way?
source share