views:

246

answers:

4

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.

A: 

Sorry, Java does not have this sort of language-level support for automatic parallelism, if you wish it, you will have to implement it yourself using libraries and threads.

caskey
+7  A: 

Java's multithreading is quite low level in this respect. The best you could do is something like this:

ExecutorService executor = Executors.newFixedThreadPool(10);
for (final Object item : collectionThingy) {
  executor.submit(new Runnable() {
    @Override
    public void run() {
      // do stuff with item
    }
  });
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

This is Java 6 code. If running on Java 5 drop the @Override annotation (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, InterruptedException and ExecutionException.

cletus
+1 for ExecutorService. Doug Lea's java.util.concurrent seems about the best approach to this in Java at this point.
Jim Ferrans
may i know will the code above block until all threads finished executed (i want wait forever,notimeout)? because i need to return result only after that? in this case, no need to force executor.shutdown() before executor.awaittermination() right?
cometta
@cometta: if you don't call `awaitTermination` then the "main" thread won't block at all waiting for the tasks, it'll carry on with whatever code comes next. And if you don't call `shutdown` then awaitTermination will never return. Both are required.
Andrzej Doyle
What Andrzej said is correct: you need both shutdown() and awaitTermination().
cletus
+1  A: 

In most cases, the added complexity wouldn't be worth the potential performance gain. However, if you needed to process a Collection in multiple threads, you could possibly use Executors to do this, which would run all the tasks in a pool of threads:

int numThreads = 4;
ExecutorService threadExecutor = Executors.newFixedThreadPool(numThreads);
for(Iterator iterator = collectionthing.iterator(); iterator.hasNext();){
    Runnable runnable = new CollectionThingProcessor(iterator.next());
    threadExecutor.execute(runnable);
}
Kaleb Brasee
+1  A: 

As part of the fork-join framework JDK7 should (although not certain) have parallel arrays. This is designed to allow efficient implementation of certain operations across arrays on many-core machines. But just cutting the array into pieces and throwing it at a thread pool will also work.

Tom Hawtin - tackline