View Full Version : What's the best way to have a specific amount of threads run and/or be reused?

01-05-2012, 10:44 AM
So let's say I have an iterator it that returns something of class CustomClass. The iterator has "many" CustomClass to go over. More over, let's say that a user inputs something that makes my program decide only 5 threads can be running at any moment by my program.

In a way, something like this is expected to happen:

for(int i = 0; i < 5 && it.hasNext(); i++) {
new Thread(it.next()).start();

Now, again only five are allowed and there are many more elements in the iterator. But after one thread is done with its run(), I want it to call it.next() and create a new thread. BUT the problem is that it is not defined in this thread. CustomClass doesn't even have a next() method of any kind. How would I make this work out so that each thread can "access" the iterator efficiently?