this is how i normally iterate a collection
for(Iterator iterator = collectionthing.iterator(); iterator.hasNext();){
I believe most of us doing this, I wonder is there any better approach than have to iterate sequentially? is there any java library..can I can make this parallel executed by multi-code cpu? =)
looking forward feedback from you all.
Java’s multithreading is quite low level in this respect. The best you could do is something like this:
This is Java 6 code. If running on Java 5 drop the
@Overrideannotation (it doesn’t apply to objects implementing interfaces in java 5 but it does in Java 6).What this does is it creates a task for each item in the collection. A thread pool (size 10) is created to run those tasks). You can replace that with anything you want. Lastly, the thread pool is shut down and the code blocks awaiting the finishing of all the tasks.
The last has at least one or two exceptions you will need to catch. At a guess,
InterruptedExceptionandExecutionException.