There seems to be a lot of fuss about multicore and java. While some people say that java support is not good enough, it definitely seems to be an area to look forward to. There seems to be many techniques to improve performance of concurrent programs.
Any tips/advices about programming in a multi core scenario is appreciated.
Look into the new Java concurrency facilities (in the
java.util.concurrentpackage) which have been added in Java 5. It provides functionality that is more high-level than regularThreads which will make it easier (and less error-prone) to write concurrent applications. The Lesson: Concurrency of The Java Tutorials would be a good place to start.So far, I’ve only used the
ExecutorService, which allows can produce thread pools, to which new tasks can be handed off in units ofRunnables orCallables (which can return values after execution asFutures), and the actual threading code is handled by theExecutorService.For example, performing some calculation using a thread pool of 2 threads, and getting the result can be as simple as:
In the above (untested) code, all I have to worry about is making a couple of
Callables andsubmiting it to theExecutorServiceand later on, using theFutures to retrieve the result.The catch is, once the
getmethod of theFutureis called, if the processing isn’t complete, the program will stop until the result of theFuturecan be retrieved. So, in this example, even if the result off2is available beforef1, the program will wait until the result off1is available.In terms of reading material, on my list of books to purchase soon is Java Concurrency in Practice by Brian Goetz, which comes up often when concurrency in Java is brought up.
The Concurrency Utilities page from the Java 5 documentation has more information as well.