Java & Usage of Multicore CPUs

The assumption is that the operating system will allocate threads to multiple cores. Assuming the OS is providing this support:

Runtime.getRuntime().availableProcessors()

This article describes writing a sort routine to use multiple cores:
http://www.javaworld.com/javaworld/jw-09-2007/jw-09-multicoreprocessing.html?page=2

Simple version:

  1. Figure out how many pieces (threads) you will break the task into.
  2. Set up a CountDownLatch for the number of pieces.
  3. When each piece (thread) finishes, decrement the CountDownLatch
  4. After you've started the threads use CountDownLatch.await() to
    wait for all the threads to finish.
  5. Do whatever you need to merge the results.